【发布时间】:2012-05-16 06:20:05
【问题描述】:
这一切在 Rails 2.3.5 中运行良好,但是当承包商公司直接升级到 2.3.14 时,突然所有集成测试都在说:
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
我的 ApplicationController 上有一个 before_filter,它设置了一堆 cookie 供一些 javascript 使用,我发现如果我注释掉除设置 cookie 值的行之外的所有行,它可以正常工作,但它不会不管我留在哪一行。
before_filter :set_cookies
def set_cookies
cookies['logged_in'] = (logged_in ? 'y' : 'n')
cookies['gets_premium'] = (gets_premium ? 'y' : 'n')
cookies['is_admin'] = (is_admin ? 'y' : 'n')
end
如果这些行中只有一条处于活动状态,则在集成测试中一切正常,否则我会收到上述错误。例如,考虑以下测试/响应:
test "foo" do
get '/'
end
$ ruby -I"lib:test" test/integration/foo_test.rb -n test_foo -v
Loaded suite test/integration/foo_test
Started
test_foo(FooTest): E
Finished in 5.112648 seconds.
1) Error:
test_foo(FooTest):
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
test/integration/foo_test.rb:269:in `test_foo'
1 tests, 0 assertions, 0 failures, 1 errors
但如果其中任何两个 cookie 设置行被注释掉,我会得到:
$ ruby -I"lib:test" test/integration/foo_test.rb -n test_foo -v
Loaded suite test/integration/foo_test
Started
test_foo(FooTest): .
Finished in 1.780388 seconds.
1 tests, 0 assertions, 0 failures, 0 errors
在开发和生产模式下运行的网站运行良好 - 这只是让测试通过的问题。此外,通过调试输出,我已验证错误不会在设置 cookie 的方法中引发,一切正常执行,错误发生在稍后的某个地方(但回溯并没有告诉我在哪里)
【问题讨论】:
标签: ruby-on-rails cookies integration-testing ruby-on-rails-2