RSPEC Controller.controller_name blank rails 6.1升级

发布于 2025-02-08 11:04:15 字数 699 浏览 1 评论 0 原文

升级到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

我现在必须明确设置控制器名称吗?

After upgrading to Rails 6.1, a test helper spec on RSpec 3.11.0 seems to be broken:

# helpers/my_helper.rb
module MyHelper
  def foobar
    controller.controller_name.to_sym
  end
end

My test looks like this

# spec/helpers/my_helper_spec.rb
describe MyHelper do
  it "foo" do
    expect(foobar).to eq(:test)
  end
end

and throws this error

  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'

Before the upgrade on Rails 6.0, controller.controller_name was just set to "test", but now it's nil.

Do I have to set a controller name now explicitly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小草泠泠 2025-02-15 11:04:16

我不是100%确定这将解决您的问题,但是尝试将 type :: helper 添加到描述语句:

describe MyHelper, type: :helper do
  it "foo" do
    expect(foobar).to eq(:test)
  end
end

I'm not 100% sure that this will fix your problem but try to add type: :helper to your describe statement:

describe MyHelper, type: :helper do
  it "foo" do
    expect(foobar).to eq(:test)
  end
end
天涯沦落人 2025-02-15 11:04:16

我发现发生了什么,比较这两行:

https://github.com/rails/rails/blob/v6.0.5/actionview/lib/lib/action_view/test_case.rb#l104

@controller = ActionView::TestCase::TestController.new

https://github.com/rails/rails/rails/rails/rails/blob/blob/blob/blob/v6.1.6/actionview/lib /Action_view/test_case.rb#l105

controller_class = Class.new(ActionView::TestCase::TestController)

在Rails 6.1中, Controller_Class 是一个匿名类,没有名称。并且因为 controller.controller_name 使用类的名称,所以它返回 nil 。我以这样的方式解决了它:

allow(controller).to receive(:controller_name).and_return("test")

但是,这个决定背后的推理很有趣。也许他们希望测试更加明确。控制器名称是什么。这是更改该行

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

@controller = ActionView::TestCase::TestController.new

https://github.com/rails/rails/blob/v6.1.6/actionview/lib/action_view/test_case.rb#L105

controller_class = Class.new(ActionView::TestCase::TestController)

In Rails 6.1, controller_class is an anonymous class with no name. And because controller.controller_name uses the class' name, it returns nil. I solved it by stubbing it like this:

allow(controller).to receive(:controller_name).and_return("test")

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

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