foreigner-matcher

Gem Version Build Status Code Climate

RSpec matcher for the Foreigner gem, inspired by Remarkable ActiveRecord matchers.

Version Information

The 0.3.x verison should accompany use of RSpec 3.0. The same version should be safe to use with RSpec 2.14.x as well.

Note that Foreigner >= 1.0.0 is Rails 3.x and higher only!

Rails 4.x + 3.x

Foreigner >= 1.0.0 use foreigner-matcher ~> 0.3.0

Foreigner ~> 0.9 use foreigner-matcher ~> 0.1.1

Rails 2.3

Use foreigner-matcher ~> 0.1.1

Installation

Please check the Version Information for the appropriate version!

For installation with bundler, add the following to the approrpriate group in your Gemfile:

gem "foreigner-matcher", "~> 0.3.0", :require => nil

For installation without bundler in Rails 2.3, add the following to your config/environments/test.rb:

config.gem "foreigner-matcher", "~> 0.1.1", :require => nil

To install the gem the old fashioned way:

gem install foreigner-matcher

In spec_helper.rb:

require 'foreigner-matcher'

Note that the entry in spec_helper.rb must follow any require for RSpec libraries.

Usage

The matcher can be used in RSpec to ensure an ActiveRecord model has the desired foreign key. The minimum argument is the table name that the subject model should have a foreign key to.

For example. Given these two models:

class User < ActiveRecord::Base
  has_many user_logins
end

class UserLogin < ActiveRecord::Base
  belongs_to user
end

And given this migration for the foreign key:

class ChangeUserIdToForeignKeyOnUserLogins < ActiveRecord::Migration
  def self.up
    add_foreign_key :user_logins, :users
  end

  def self.down
    remove_foreign_key :user_logins, :users
  end
end

The spec would look like this:

describe UserLogin do
  it { should have_foreign_key_for(:users) }
end

In addition to the table name, you can include any options that add_foreign_key (see Foreigner) accepts. Some more examples using the same models:

it { should have_foreign_key_for(:users, :dependent => :delete) }
it { should have_foreign_key_for(:users, :column => "unique_user_id", :name => "user_logins_unique_user_id_fk") }
it { should_not have_foreign_key_for(:users, :dependent => :nullify) }

A Note on Table Names

These examples will also work passing :user as the table name. Why? The example has a belongs_to relationship, and my feeling is that it reads better to say "it should have foreign key for user". This is just my taste; use what makes sense to you!

My Testing Environment

The project is tested on Travis-CI. The rubies and databases that the matcher is regularly verified with can be seen in .travis.yml

The current gems that are tested:

  • ActiveRecord ~> 4.1.0, Rspec ~> 3.0.0, Foreigner >= 1.00
  • ActiveRecord ~> 4.1.0, Rspec ~> 2.14.0, Foreigner >= 1.00
  • ActiveRecord ~> 4.0.0, Rspec ~> 3.0.0, Foreigner >= 1.00
  • ActiveRecord ~> 4.0.0, Rspec ~> 2.14.0, Foreigner >= 1.00
  • ActiveRecord ~> 3.2.0, Rspec ~> 3.0.0, Foreigner >= 1.00
  • ActiveRecord ~> 3.2.0, Rspec ~> 2.14.0, Foreigner >= 1.00

Copyright (c) 2011-2014 Cameron Dykes. See LICENSE.txt for further details.