Last Updated: February 25, 2016
·
917
· dlikhten

Testing Factories

We noticed that every once in a while our factories would fail to build. And naturally that would cause most of our tests to fail.

So this little test will ensure that all factories are valid and we run this before any other spec.

spec/factories/factories_spec.rb

require 'spec_helper'

describe "Factory" do
  FactoryGirl.factories.map(&:name).each do |factory_name|
    context "#{factory_name}" do
      subject { Factory.create(factory_name) }
      it {
        subject.persisted?.should be_true
        subject.errors.should be_empty
        subject.valid?.should be_true
      }
    end
  end
end