Skip to content

Instantly share code, notes, and snippets.

@agirorn
Created October 31, 2012 17:21
Show Gist options
  • Save agirorn/3988456 to your computer and use it in GitHub Desktop.
Save agirorn/3988456 to your computer and use it in GitHub Desktop.
ActiveModel::Lint for RSpec without loading Rails
# 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
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
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