【问题标题】:RSpec can't find Factorys from FactorygirlRSpec 找不到 Factorygirl 的工厂
【发布时间】:2011-05-17 05:49:46
【问题描述】:

我将在我的 Rails3 项目中将 RSpec 与 Factory girl 一起使用。我已经安装了工厂女孩,但没有找到我有这个错误的工厂

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

宝石文件:

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

谢谢

【问题讨论】:

  • 问题出在 factory_girl 上。当我使用 factory_girl_rails 时一切正常。

标签: ruby-on-rails rspec bdd factory-bot


【解决方案1】:

你有没有尝试添加类似的东西

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

在 spec_helper 中? 那是为我做的

【讨论】:

  • 这为我修复了它 - 其他解决方案没有修复它
【解决方案2】:

如果您的 rspec 配置正在加载规范/支持下的所有内容,那么您可以将您的工厂目录放在那里。

看到这个帖子:http://www.codeography.com/2010/04/30/using-factory_girl-and-rspec-with-rails-3.html

【讨论】:

    【解决方案3】:

    将“factory_girl_rails”gem 添加到您的 :test, :development 组下的 Gemfile 中,如下所示:

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

    在 Rails 3 中,您需要添加该 gem 才能使其工作。希望对您有所帮助。

    【讨论】:

    • ...事实上,如果您愿意,您可以删除 factory_girl 行本身。它是 factory_girl_rails 的依赖,所以会自动包含进来。
    【解决方案4】:

    您的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
    

    【讨论】:

    • 感谢您的帮助。我在 config\application.rb 中添加了这个,但我有同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多