AssociationTypeMismatch

AssociationTypeMismatch

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

问题描述

这最近导致一些挫折...



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



MyModel(#65776650)expected,got MyModel(#28190030)(ActiveRecord :: AssociationTypeMismatch)



当存在关联引用时 - 就像Factory创建的对象不同于真实的对象。有关详情,请参阅此问题:



我已经逐渐将工厂调用更改为真正的Model.create或mock_model调用。这将是很高兴继续使用工厂的女孩...我想知道是否有一些我可能做错了吗?



谢谢

解决方案


您应该更改您的Gemfile将是这样:

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

然后如果你使用Spork,使你的spec / spec_helper.rb文件看起来像这样:

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


This has been causing some frustration recently...

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

MyModel(#65776650) expected, got MyModel(#28190030) (ActiveRecord::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

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?

Thank you

解决方案

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

To expand on Jonas' answer.

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 11:46