Rack::Test 导致 ActiveRecord::AssociationTypeMismatch

发布于 2024-10-29 03:31:51 字数 655 浏览 0 评论 0原文

我在运行所有规格时遇到问题。

    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 技术交流群。

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

发布评论

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

评论(2

就是爱搞怪 2024-11-05 03:31:51

rails.application的源代码:

# File railties/lib/rails.rb, line 34
def application
  @@application ||= nil
end

这意味着rails.application每次都会返回相同的对象。也许这是问题 - 在同一Rails应用程序与对象冲突上运行多个测试。

这样的设置测试:

def app
  Rails::Application
end

某些教程像 href =“ http://jackhq.tumblr.com/post/100555556645/ususe-rack-test-for-rails-and-hards-and-h- rails-and--restful-apis” rel =“ nofollow”>其他

def app
  ActionController::Dispatcher.new
end

其中为app的每个调用创建新对象。

编辑:刚从日志中注意到actioncontroller :: dispatcher.new被标记为已弃用。

From source code for Rails.application:

# File railties/lib/rails.rb, line 34
def application
  @@application ||= nil
end

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:

def app
  Rails::Application
end

Whereas others do it like this:

def app
  ActionController::Dispatcher.new
end

Both of which create new object for each call to app.

EDIT: Just noticed from logs that ActionController::Dispatcher.new is marked as deprecated.

满意归宿 2024-11-05 03:31:51

我不再有这个问题了。我更新了我的宝石。 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.

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