Skip to content

Instantly share code, notes, and snippets.

@dmitriy-kiriyenko
Created February 23, 2012 13:37
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmitriy-kiriyenko/1892874 to your computer and use it in GitHub Desktop.
Save dmitriy-kiriyenko/1892874 to your computer and use it in GitHub Desktop.
ActiveModel lint tests for rspec
# put the file into spec/support
shared_examples_for "ActiveModel" do
include ActiveModel::Lint::Tests
# to_s is to support ruby-1.9
ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
example m.gsub('_',' ') do
send m
end
end
def model
subject
end
end
# implicit subject
describe MyLooksLikeModelClass do
it_behaves_like "ActiveModel"
end
# explicit subject
describe "my looks like model class" do
subject { MyLooksLikeModelClass.new }
it_behaves_like "ActiveModel"
end
@dmitriy-kiriyenko
Copy link
Author

You can read more about active model lint tests in "Crafting Rails applications".

The easiest way to create ActiveModel compliant class is to use ActiveAttr.

@ronan-mch
Copy link

Thanks!

@anantalft
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment