Created
October 31, 2012 17:21
-
-
Save agirorn/3988456 to your computer and use it in GitHub Desktop.
ActiveModel::Lint for RSpec without loading Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# hijacked from :http://library.edgecase.com/activemodel-lint-test-for-rspeca | |
# | |
# Usage: | |
# | |
# describe SomeModelClass do | |
# it_should_behave_like "ActiveModel" do | |
# let(:model) { SomeModelClass.new } | |
# end | |
# end | |
shared_examples_for "ActiveModel" do | |
require 'test/unit/assertions' | |
require 'active_model/lint' | |
require 'active_support/core_ext/object/blank' | |
include Test::Unit::Assertions | |
include ActiveModel::Lint::Tests | |
# to_s is to support ruby-1.9 | |
ActiveModel::Lint::Tests.public_instance_methods.map(&:to_s).grep(/^test/).each do |method| | |
example method.gsub('_',' ') do | |
send method | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'support/active_model_lint' | |
require 'plain_ruby_object_model' | |
describe PlainRubyObjectModel do | |
it_should_behave_like "ActiveModel" do | |
let(:model) { PlainRubyObject.new } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlainRubyObject | |
include ActiveModel::Conversion | |
extend ActiveModel::Naming | |
def to_model | |
self | |
end | |
def persisted? | |
false | |
end | |
def valid? | |
true | |
end | |
def errors | |
obj = Object.new | |
def obj.[](key) [] end | |
def obj.full_messages() [] end | |
obj | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment