dm-rspec2 (RSpec 2 matchers for DataMapper)

by svs

based on the work of Potapov Sergey at github.com/greyblake/dm-rspec

A set of rspec matchers to test DataMapper models like you test ActiveRecord models with rspec-rails that work with and use RSpec 2 matcher DSL

Installation

gem install dm-rspec2

Usage

Add the following to your spec_helper:

require 'dm-rspec2'

RSpec.configure do |config|
  config.include(DataMapper::Matchers)
end

In your spec files you can use the following matchers to test appropriate DataMapper’s validations:

Examples

Assume you have the following data mapper models:

class Book
  include DataMapper::Resource
  property :id, Serial
  property :name, String
  belongs_to :author
  has n, :genres, :through => Resource
  validates_presence_of :name
end

class Author
  include DataMapper::Resource
  property :id, Serial
  has n, :books
end

class Genre
  include DataMapper::Resource
  property :id, Serial
  property :name, String
  has n, :books, :through => Resource
end

Your specs can contain the following:

specify {Book.should have_property :name}
specify {Book.should belong_to :author}
specify {Author.should have_many :books}
specify {Genre.should  have_many_and_belong_to :books}
specify {Book.should  have_many_and_belong_to :genres}

specify {Book.should validate_presence_of :name}

it 'has errors' do
  book = Book.new(:name => 'fails on two validations')
  book.valid?
  book.should have(2).errors_on(:name)
end

They can look like below as well:

describe Book do
  it { should have_property :name   }
  it { should  belongs_to   :author }
end

TODO

Update all the matchers to RSpec 2 format (see have_property.rb for an example)

Contributing to dm-rspec

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see www.gnu.org/licenses/lgpl.txt