Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec can't find Factorys from Factorygirl

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

like image 313
ThreeFingerMark Avatar asked Nov 29 '10 11:11

ThreeFingerMark


2 Answers

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
like image 109
nathanvda Avatar answered Sep 18 '22 17:09

nathanvda


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.

like image 23
GreenPlastik Avatar answered Sep 18 '22 17:09

GreenPlastik