RSPEC Controller.controller_name blank rails 6.1升级
升级到Rails 6.1后,RSPEC 3.11.0上的测试助手规格似乎被打破了:
# helpers/my_helper.rb
module MyHelper
def foobar
controller.controller_name.to_sym
end
end
我的测试看起来像这样
# spec/helpers/my_helper_spec.rb
describe MyHelper do
it "foo" do
expect(foobar).to eq(:test)
end
end
错误扔向此错误
1) MyHelper foo
Failure/Error: controller.controller_name.to_sym
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./app/helpers/my_helper.rb:5:in `foobar'
,并在Rails 6.0上的升级之前, Controller.controller_name
将此 。 “ Test”
,但现在是 nil
。
我现在必须明确设置控制器名称吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是100%确定这将解决您的问题,但是尝试将
type :: helper
添加到描述语句:I'm not 100% sure that this will fix your problem but try to add
type: :helper
to yourdescribe
statement:我发现发生了什么,比较这两行:
https://github.com/rails/rails/blob/v6.0.5/actionview/lib/lib/action_view/test_case.rb#l104
https://github.com/rails/rails/rails/rails/rails/blob/blob/blob/blob/v6.1.6/actionview/lib /Action_view/test_case.rb#l105
在Rails 6.1中,
Controller_Class
是一个匿名类,没有名称。并且因为controller.controller_name
使用类的名称,所以它返回nil
。我以这样的方式解决了它:但是,这个决定背后的推理很有趣。也许他们希望测试更加明确。控制器名称是什么。这是更改该行
I found out whats going on, compare these two lines:
https://github.com/rails/rails/blob/v6.0.5/actionview/lib/action_view/test_case.rb#L104
https://github.com/rails/rails/blob/v6.1.6/actionview/lib/action_view/test_case.rb#L105
In Rails 6.1,
controller_class
is an anonymous class with no name. And becausecontroller.controller_name
uses the class' name, it returnsnil
. I solved it by stubbing it like this:However, the reasoning behind this decision would be interesting. Maybe they wanted tests to be more explicit what the controller name is. This is the commit that changed that line https://github.com/rails/rails/commit/3ec8ddd0cf3851fd0abfc45fd430b0de19981547