AssociationTypeMismatch 和 FactoryGirl [英] AssociationTypeMismatch and FactoryGirl

查看:13
本文介绍了AssociationTypeMismatch 和 FactoryGirl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这最近引起了一些挫败感......

This has been causing some frustration recently...

似乎在我的黄瓜测试中使用工厂,在某些情况下会导致 AssociationTypeMismatch 错误,例如:

It seems that using Factories in my cucumber tests, in some situations causes AssociationTypeMismatch errors such as:

MyModel(#65776650) 预期,得到 MyModel(#28190030) (ActiveRecord::AssociationTypeMismatch)

当有关联引用时,这些似乎会发生 - 好像工厂创建的对象与真实对象不同.有关详细信息,请参阅此问题:黄瓜重复类问题:AssociationTypeMismatch

These seem to happen when there is an association reference - as if the Factory created object is different to the real one. See this question for more details: Cucumber duplicate class problem: AssociationTypeMismatch

我一直在逐渐将工厂调用更改为真正的 Model.create 或 mock_model 调用.继续使用工厂女孩会很好......我想知道我是否做错了什么?

I have been gradually changing Factory calls to real Model.create or mock_model calls. It would be nice to keep using Factory girl... I wonder if there is something I may have done wrong?

谢谢

推荐答案

我在 Rails 3.1.0 rc5 上遇到过这种情况,并且可以正常工作.

I had this happening with me on Rails 3.1.0 rc5, and got it working.

扩展乔纳斯的回答.

你应该把你的 Gemfile 改成这样:

You should change your Gemfile to be like this:

gem 'factory_girl', '~> 2.0.0', :require => false
gem 'factory_girl_rails', '~> 1.1.0', :require => false

然后,如果您使用的是 Spork,请让您的 spec/spec_helper.rb 文件如下所示:

And then if you are using Spork, make your spec/spec_helper.rb file look like this:

Spork.each_run do
 require 'factory_girl'
 require 'factory_girl_rails'
end

这篇关于AssociationTypeMismatch 和 FactoryGirl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆