Skip to content

Testing all Factories (with RSpec)

AndreyChernyh edited this page Jan 29, 2013 · 27 revisions

To make sure that your models are valid you can automatically test all of your factories with the following code:

require 'spec_helper'

describe 'validate FactoryGirl factories' do
  FactoryGirl.factories.each do |factory|
    context "with factory for :#{factory.name}" do
      subject { FactoryGirl.build(factory.name) }

      it "is valid" do
        is_valid = subject.valid?
        is_valid.should be_true, subject.errors.full_messages.join(',')
      end
    end
  end
end

Clone this wiki locally