在Rails3中让factory_girl工作

7

我无论如何都无法使factory_girl在Rails3中工作。我创建了一个全新的应用程序。我的Gemfile:

gem "rspec"
gem "rspec-rails"
gem "factory_girl_rails"

application.rb 中的生成器如下所示:

  config.generators do |g|
      g.test_framework :rspec, :fixture => true, :views => false, :fixture_replacement => :factory_girl
  end

然后,使用生成器创建一个新模型:

> rails g model Addon name:string

  invoke  active_record
  create    db/migrate/20101223205918_create_addons.rb
  create    app/models/addon.rb
  invoke    rspec
  create      spec/models/addon_spec.rb
   error      factory_girl [not found]

我错过了什么?当然我运行了bundle install... 我尝试查找,但是找不到有关factory_girl和rails3的合适文档。

3个回答

11

1
不需要在Rails 3.2中使用...这已经有点过时了。 - Ghoti

3

截至今日,这是Rails 3.2的正确做法:

Rails.application.config.generators do |g|
  g.test_framework :rspec, fixture: true
  g.fixture_replacement :factory_girl, dir: 'spec/factories'
end

刚刚尝试了一下test/unit,但是没有成功。这是只有RSpec才支持的语法吗?我使用了以下代码:g.test_framework :test_unit, fixture: trueg.fixture_replacement :factory_girl - Leo

2

有些用户可能正在使用factory_girl。请确保您使用的是factory_girl_rails


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接