AssociationTypeMismatch 和 FactoryGirl
这最近引起了一些挫折......
似乎在我的黄瓜测试中使用工厂,在某些情况下会导致 AssociationTypeMismatch
错误,例如:
MyModel(#65776650)预期,得到 MyModel(# 28190030) (ActiveRecord::AssociationTypeMismatch)
这些似乎在存在关联引用时发生 - 就好像工厂创建的对象与真实对象不同一样。有关更多详细信息,请参阅此问题: Cucumber 重复类问题:AssociationTypeMismatch
我一直在逐渐改变工厂调用真实的 Model.create 或 mock_model 调用。继续使用Factory Girl就好了……我想知道我是否做错了什么?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我在 Rails 3.1.0 rc5 上遇到了这种情况,并且让它正常工作。
扩展乔纳斯的答案。
您应该将 Gemfile 更改为如下所示:
然后,如果您使用 Spork,则使您的 spec/spec_helper.rb 文件如下所示:
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:
And then if you are using Spork, make your spec/spec_helper.rb file look like this:
如果 ActiveSupport 卸载并重新加载您引用的常量,似乎会发生这种情况。
我在 Rspec/Capybara 上也经历过同样的事情,有帮助的是不同的事情的混合:
我正在使用 Spork (测试服务器),这似乎使得这个事情变得越来越困难。
如果您使用的是测试服务器,请评估是否应该放置 ', :require =>;在 gemspec 中的factory_girl 之后为 false。
此 Google 网上论坛帖子也涵盖了该主题,
请告知我们如果其中有任何帮助的话。
It seems to happen if ActiveSupport unloads and reloads a constant that you have a reference to.
I've experienced the same with Rspec/Capybara, and what helped was a mixture of different things:
I'm using Spork (a test server), which seems to make this stuff increasingly difficult.
If you are using a test server, evaluate whether you should put ', :require => false' after factory_girl in your gemspec.
The topic is also covered in this google groups thread
Please let us know if any of this helped.
如果您使用 Spork,请确保在重新加载模型之后重新加载工厂。
例如
If you're using Spork, make sure to reload your factories after reloading your models.
E.g.
发生这种情况是因为按照 Spork 的要求,cache_classes 为 false。 Capybara 会为每个请求重新加载 Rails 类(或者,正确地说,Rails 的重新加载器中间件会这样做,这在正常测试中是不需要的),这让工厂感到害怕(确切的原因,我不确定)。您可以重新加载它们,或者只是在 Spork 之外运行您的 Capybara 规范。
因此,您需要两件事:仅在 Spork 之外运行 Capybara,并仅针对 Spork 将 cache_classes 设置为 false。
为了只在 Spork 之外运行 Capybara,我有一个 Guardfile,它运行 Spork 之外的规范/请求中的规范以及 Spork 内部的其他规范:
https://gist.github.com/1731900
然后,在 config/environments/test.rb 中:
你的 Capybara 规格会慢一些,因为它们需要启动轨道,但一切都会正常工作。
This happens because cache_classes is false, as is required by Spork. Capybara reloads Rails classes for every request (or, to be correct, Rails' reloader middleware does, which is not called for normal tests), and this freaks out the factories (exactly why, I'm not sure). You can either reload them, or simply run your Capybara specs outside of Spork.
So you need two things: to only run Capybara outside of Spork, and to set cache_classes to false only for Spork.
To only run Capybara outside of Spork, I have a Guardfile that runs specs in spec/requests outside of Spork and other specs inside of Spork here:
https://gist.github.com/1731900
Then, in
config/environments/test.rb
:Your Capybara specs will be a bit slower, as they need to boot rails, but everything will Just Work.
我在重新加载工厂定义方面取得了一些成功,尝试如下操作:
I had some success with reloading the Factory definitions try something like this:
当我将由其他工厂继承的“class”选项传递给我的工厂时,我遇到了这个问题:
我能找到的唯一解决方案就是不这样做。
I ran into this issue when I passed the "class" option to my factory that was inherited by other factories:
The only solution I could find was to simply not do this.
我遇到了同样的问题,并花了大约十个小时尝试该线程和网络上其他地方的每个解决方案。我开始删除大量代码,试图使其尽可能接近我的另一个应用程序,在该应用程序中我无法重现问题。最后,我在 spec_helper 文件中发现了一些辅助函数:
一个
sign_in
辅助函数,旨在在控制器和请求规范中工作。确实如此——只是不是用勺子。当我删除水豚助手后,问题就解决了:I ran into this same issue and spent probably ten hours trying every solution on this thread and everywhere else on the web. I started ripping out huge chunks of code trying to get it as close to another app of mine in which I couldn't reproduce the problem. Finally, I came across some helper functions in my spec_helper file:
A
sign_in
helper intended to work both in controller and request specs. And it does, sort of--just not with spork. When I removed the capybara helpers the issue was resolved: