这一切在 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 的方法中抛出,一切正常,错误发生在稍后的某个地方(但回溯没有告诉我在哪里)