Index ¦ Archives ¦ RSS

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 }
end

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 }
end

© James Lim. Built using Pelican. Theme by Giulio Fidente on github.