0
votes

I'm trying to test my factoryGirl factories:

#spec/factories/post.rb
FactoryGirl.define do
 factory :post do |p|
  p.title "Title"
  p.body "Body"
 end
end

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

according to this

But i have this error:

in `block in <top (required)>': uninitialized constant Factory (NameError)
1
I just tried the rspec code in one of my model spec files and it worked fine. Does that work for you?? It's not DRY this way, but it might help diagnose the problem. - Neil Billingham

1 Answers

1
votes

Maybe try this:

Create a separate file in /spec called factories_spec.rb like this

require 'spec_helper'

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

remember to include the require 'spec_helper' line