Skip to content
golive edited this page Sep 7, 2012 · 1 revision

Test all factories

Tip from Thoughtbot

# spec/factories_spec.rb

FactoryGirl.factories.map(&:name).each do |factory_name|
  describe "The #{factory_name} factory" do
    it 'is valid' do
      build(factory_name).should be_valid
    end
  end
end

If you use rake to run your tests, one way to take care of this is by adding a pre-requisite to your rake spec task:

# Rakefile

desc 'Run factory specs.'
RSpec::Core::RakeTask.new(:factory_specs) do |t|
  t.pattern = './spec/factories_spec.rb'
end

task spec: :factory_specs
Clone this wiki locally