Posted on Thu 28 March 2013

Some Custom Matchers for RSpec with Shoulda

It should have a valid factory

I use Factory Girl in my tests, and I have found it useful to check that I have valid factories for each model in my tests/specs. Here is a matcher you can use for that purpose.

Add this file to spec/support. Then in your specs, just use

describe User do
  it { should have_a_valid_factory }

It should validate the existence of X

In some of my models, I have validations that ensure existence of a given foreign key i.e. the foreign key must refer to an existing record. You can use the validates_existence gem, or write your own. Here is a matcher for validates_existence_of.

In your specs, just use

describe Membership do
  it { should validate_existence_of :user }

Category: Software

Tags: ruby, rspec, shoulda, factory_girl

Jim LimGitHubLinkedInStackOverflowontoplist ⋅ Theme by Giulio Fidente.