工厂女孩与 Spork 差异的关联

发布于 2024-11-28 06:50:53 字数 880 浏览 5 评论 0 原文

我正在尝试将 Factory Girl 与 Spork 一起用于 RSpec。每当我在没有 Spork 的情况下运行测试时,一切都会通过,但是当我使用 Spork 运行测试时,所有尝试创建依赖于另一个工厂的工厂实例的测试都会失败。例如:

3) Invitation has a correctly formed body
   Failure/Error: request = FactoryGirl.create(:request, ....)
   NoMethodError:
     undefined method `user=' for #<Request:0x007f86b6a87890>

我的 factories.rb 代码看起来大致像这样

FactoryGirl.define do
  factory :user do
    sequence(:first_name) { |n| "first_name#{n}" }
    sequence(:last_name) { |n| "last_name#{n}" }
  end

  factory :request do
    association :user, :factory => :user, :is_walker => false
  end
end

我的代码似乎只有在存在关联时才会中断,然后它尝试调用 :user。为什么会发生这种情况?

这是我正在使用的版本

gem 'rails', '3.0.7'
gem 'rspec-rails', '2.6.1'
gem 'spork', '0.9.0.rc5'
gem 'factory_girl_rails', '1.0'

I'm trying to use Factory Girl for RSpec with Spork. Whenever I run my tests without spork, everything passes, but when I run it with Spork, all the tests that try to create an instance of a factory that depends on another factory, fail. For example:

3) Invitation has a correctly formed body
   Failure/Error: request = FactoryGirl.create(:request, ....)
   NoMethodError:
     undefined method `user=' for #<Request:0x007f86b6a87890>

And my factories.rb code looks roughly like this

FactoryGirl.define do
  factory :user do
    sequence(:first_name) { |n| "first_name#{n}" }
    sequence(:last_name) { |n| "last_name#{n}" }
  end

  factory :request do
    association :user, :factory => :user, :is_walker => false
  end
end

My code only seems to break when there is an association, and then it tries to call the setter on the :user. Why might this be happening?

Here are the versions I'm using

gem 'rails', '3.0.7'
gem 'rspec-rails', '2.6.1'
gem 'spork', '0.9.0.rc5'
gem 'factory_girl_rails', '1.0'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

予囚 2024-12-05 06:50:53

您的 Spork.each_run 块中是否有 require 'factory_girl_rails

我在 gemfile 中使用了以下内容:

  gem "factory_girl_rails", :require => false

在 spec_helper.rb 中:

Spork.each_run do

  require 'factory_girl_rails'
  FactoryGirl.factories.clear
  FactoryGirl.reload

end

不确定它是否有帮助,但值得一试。

Do you have require 'factory_girl_rails in your Spork.each_run block?

I have use the following in my gemfile:

  gem "factory_girl_rails", :require => false

in spec_helper.rb:

Spork.each_run do

  require 'factory_girl_rails'
  FactoryGirl.factories.clear
  FactoryGirl.reload

end

Not sure if it will help but it's worth a shot.

℡寂寞咖啡 2024-12-05 06:50:53

是否真的有任何具有关联的测试或任何尝试创建 :request 工厂的测试?我从来没有使用过 spork,但事实上它在没有它的情况下也能工作,这让我想知道 spork/factory_girl 是否正在争夺“请求”......值得研究。

Is it really any tests that have an association or any tests that try to create a :request factory? I've never used spork, but the fact it works without it makes me wonder if spork/factory_girl are fighting over "Request"... worth looking into.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文