Rack::Test 导致 ActiveRecord::AssociationTypeMismatch
我在运行所有规格时遇到问题。
ActiveRecord::AssociationTypeMismatch: Affiliate(#2154746360) expected, got Affiliate(#2199508660)
看来我的模型被加载了两次。
我已经隔离了 Rack::Test 定义“app”方法的要求引入的问题。
require 'rack/test'
include Rack::Test::Methods
# app method is needed for rack-test
def app
Rails.application
end
如果我注释掉 Rails.application,我的机架规格将不起作用,但我的所有其他规格都可以正常工作。在“app”方法中使用 Rails.application
会引入上述错误。
如果我单独运行我的规格,一切正常。我正在使用 Spork 预加载我的环境,并且我认为模型首先由 Spork 加载,然后在我的“app”方法中调用 Rails.application
时重新定义它们。
关于如何解决这个问题有什么想法吗?我不确定是否有另一种方法可以在“app”方法中设置我的 Rails 应用程序。
I have a problem when running all of my specs.
ActiveRecord::AssociationTypeMismatch: Affiliate(#2154746360) expected, got Affiliate(#2199508660)
It would appear that my models are being loaded twice.
I have isolated the problem to be introduced with Rack::Test's requirement to define an "app" method.
require 'rack/test'
include Rack::Test::Methods
# app method is needed for rack-test
def app
Rails.application
end
If I comment out Rails.application
my rack specs do not work, but all of my other specs work fine. The use of Rails.application
in the "app" method introduces the error above.
If I run my specs individually, everything works. I am preloading my environment with Spork and I think that the models are loaded first by Spork and then they are redefined when Rails.application
is called in my "app" method.
Any ideas on how I can resolve this problem? I am not sure if there is another way to set my Rails app in the "app" method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从
rails.application
的源代码:这意味着
rails.application
每次都会返回相同的对象。也许这是问题 - 在同一Rails应用程序与对象冲突上运行多个测试。这样的设置测试:
某些教程像 href =“ http://jackhq.tumblr.com/post/100555556645/ususe-rack-test-for-rails-and-hards-and-h- rails-and--restful-apis” rel =“ nofollow”>其他
其中为
app
的每个调用创建新对象。编辑:刚从日志中注意到
actioncontroller :: dispatcher.new
被标记为已弃用。From source code for
Rails.application
:This means that
Rails.application
returns the same object every time. Maybe this is the problem - running multiple tests on the same Rails app clashes with objects.Some tutorials set tests up like this:
Whereas others do it like this:
Both of which create new object for each call to
app
.EDIT: Just noticed from logs that
ActionController::Dispatcher.new
is marked as deprecated.我不再有这个问题了。我更新了我的宝石。 Rails 从 3.0.5 更新到 3.0.7,我猜这可能是解决了我的问题的 gem 更新。无论哪种方式,使用新版本的 gems,我的问题都得到了解决。
I am no longer having this problem anymore. I updated my gems. Rails was updated from 3.0.5 to 3.0.7 and I would guess that may have been the gem update that fixed my problem. Either way with newer versions of gems, my problem is fixed.