The EdgeCase Library Need Development? Check us out!

ActiveModel Lint Test for RSpec

Posted 30 October 2010

Author Mike Enriquez

ActiveModel comes with a set of Test::Unit tests that you can run against your model to ensure that it is compliant with the ActiveModel API. We're using RSpec in our project and didn't want to get Test::Unit setup just for these tests, so we found a way to run them with RSpec.

# spec/support/active_model_lint.rb
# adapted from rspec-rails:
http://github.com/rspec/rspec-rails/blob/master/spec/rspec/rails/mocks/mock_model_spec.rb

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

This defines a shared example and magically takes the lint tests and creates RSpec examples with them. The lint tests look for a model method, which we define to return the subject. Below is an example of how to use the shared example:

describe MyActiveModelClass do
  it_should_behave_like "ActiveModel"
end

The lint tests simply check if you have the right methods defined; it is up to you to make sure the behavior of those methods are correct. The source for the lint test can give you some insight on how to do this.