我有使用 RSpec 3.4.0 测试的 Rails 4.1.16 API 应用程序,并且在测试不同模块中调用相同名称的类时遇到问题。
结构是:
app/controllers/bar/notifications_controller.rb
class Bar::NotificationsController < ApiController
...
end
和不同模块中具有相同名称的控制器:
app/controllers/foo/bar/notifications_controller.rb
module Foo
class Bar::NotificationsController < ApiController
...
end
end
The Foo
是一个新模块,尚未进行测试。
添加后,所有对应的控制器测试为旧的Bar::NotificationsController
开始失败。
规格文件:
spec/controllers/bar/notifications_controller_spec.rb
require 'spec_helper'
describe Bar::NotificationsController, type: :controller do
...
end
该规范文件中的所有测试都失败并出现相同的错误:
RuntimeError:
@controller is nil: make sure you set it in your test's setup method.
当我更改控制器名称时,问题不存在Foo
module:
app/controllers/foo/bar/foo_notifications_controller.rb
module Foo
class Bar::FooNotificationsController < ApiController
...
end
end
我已经尝试在规范文件的顶部添加require 'bar/notifications_controller'
并使用类名作为字符串describe "Bar::NotificationsController, type: :controller
但它没有解决问题(同样的错误)。
为什么会发生这种情况?解决办法是什么?
我想相信有一件小事我还没有尝试过,我不必为了让规范通过而用无意义的名称污染我的代码和结构。
非常感谢您的帮助!