Thursday, September 27, 2012

RSpec matcher for model attributes

Have you ever wanted to check that the attributes of a model have been defined correctly?

You may have created a model Store as follows:

class Create'Stores < ActiveRecord::Migration
  def change
    create_table :stores do |t|
      t.string :title
      t.string :description, :null => false
      t.string :url, :limit => 1024
      t.boolean :open
    end
  end
end
Imagine that there was a have_attribute matcher, that allowed you to write specs for each of these attributes as follows:
require 'spec_helper'

describe Store do
  it { should have_attribute(:title) }
  it { should have_attribute(:description).with(:type => :string, :null => false) }
  it { should have_attribute(:url).with(:type => :string, :limit => 1024) }
  it { should have_attribute(:open).with(:type => :boolean) }
end
Good news! Such a matcher now exists in the form a gem. Look at https://rubygems.org/gems/rspec_attribute_matchers.

To make this matcher available in your model specs, do the following:

Gemfile
group :development do
  ...
  gem 'rspec_attribute_matchers', '~> 0.0.0'
  ...
end

spec/spec_helper.rb
require 'rspec_attribute_matchers'

RSpec.configure do |config|
  ...
  config.include RSpecAttributeMatchers, :type => :model
  ...
end