RSpec can't find Factorys from Factorygirl
Asked Answered
S

4

8

i will use RSpec with Factory girl in my Rails3 Project. I have installed factory girl but it don't find the factorys i have this error

Failure/Error: Factory.build(:user).should_be valid
No such factory: user

spec/factories/user_factory.rb :

Factory.define :user do |u|
  u.username 'otto'
end

spec/spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
end

Gemfile:

group :development, :test do
   gem 'webrat'
   gem "cucumber-rails"
   gem "rspec-rails"
   gem "rspec"
   gem "autotest"
   gem 'factory_girl'
end

Thanks

Stonework answered 29/11, 2010 at 11:35 Comment(1)
The Problem is with factory_girl. When i use factory_girl_rails everything work fine.Stonework
U
10

Do you have the following lines in your config\application.rb:

# Configure generators values.
config.generators do |g|
  g.test_framework  :rspec, :fixture => true
  g.fixture_replacement :factory_girl, :dir=>"spec/factories"
end
Underscore answered 29/11, 2010 at 12:22 Comment(1)
Thank you for the help. I have add this in the config\application.rb but i have the same error.Stonework
M
5

Add the 'factory_girl_rails" gem to your Gemfile under your :test, :development groups, as follows:

group :development, :test do
   gem 'webrat'
   gem "cucumber-rails"
   gem "rspec-rails"
   gem "rspec"
   gem "autotest"
   gem 'factory_girl'
   gem 'factory_girl_rails'
end

In Rails 3, you need to add that gem to make it work. Hope it helps.

Melise answered 9/12, 2010 at 6:15 Comment(1)
... and in fact you can remove the factory_girl line itself should you wish to. It's a dependency of factory_girl_rails, so will be included automatically.Fourlegged
I
2

have you tried adding something like

Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}

in the spec_helper? that did it for me

Internment answered 16/12, 2010 at 15:11 Comment(1)
this fixed it for me - the other solution did not fix itCotton
E
0

If your rspec config is loading everything under spec/support then you can put your factories dir there.

See this post: http://www.codeography.com/2010/04/30/using-factory_girl-and-rspec-with-rails-3.html

Eager answered 17/6, 2011 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.