简体   繁体   English

Shoulda / Minitest redirect_to {url_helper}不能在没有哈希的情况下工作

[英]Shoulda/Minitest redirect_to { url_helper } not working without hash

Using Rails 4.2, Minitest, and Shoulda... 使用Rails 4.2,Minitest和Shoulda ...

student_phone_numbers_controller_test.rb student_phone_numbers_controller_test.rb

class StudentPhoneNumbersControllerTest < ActionController::TestCase
  context "deleting to remove" do
    context "when the phone number doesn't belong to the student" do
      setup do
        log_in_as(teachers(:schmidt))
        delete :remove,
          format: "js",
          student_id: students(:two).id,
          id: phone_numbers(:one_one).id
      end

      should use_before_action :logged_in_teacher
      should use_before_action :set_student
      should respond_with :redirect
      should redirect_to { student_path students(:two) }
    end
  end
end

New to Shoulda and Rails testing in general, and my code seems to parallel the Minitest documentation for redirect_to . 一般而言,Shoulda和Rails测试是新手,我的代码似乎与Minitest文档的redirect_to相似。 However, I'm banging into this error when running the test... 但是,我在运行测试时遇到了这个错误...

$ rake test:controllers
rake aborted!
ArgumentError: wrong number of arguments (0 for 1)

...pointing at the should redirect_to line. ...指向should redirect_to行。 I assume it wants the controller/action hash? 我假设它想要控制器/动作哈希?

The following line works as expected: 以下行按预期工作:

should redirect_to(controller: :students, action: :show) { student_url students(:two) }

...as does this... ...就像...

should redirect_to({}) { student_url students(:two) } 

The first fix is totally redundant. 第一个解决方案是完全多余的。 The second seems superfluous. 第二个似乎多余。 I realize the source for this method doesn't have a default value for the single argument. 我意识到此方法的源没有单个参数的默认值。 Should it? 应该是?

Or, am I missing something otherwise obviously about the purpose/workings of this helper? 或者,我是否明显缺少有关此助手的目的/工作的其他信息?

This is actually a bug in the documentation and in fact was raised as an issue just recently: https://github.com/thoughtbot/shoulda-matchers/issues/788 . 这实际上是文档中的错误,实际上是最近才提出来的一个问题: https : //github.com/thoughtbot/shoulda-matchers/issues/788 The first argument is meant to be a human-readable representation of the route; 第一个参数是路线的人类可读的表示形式; it's inserted into the name of the test and thus will show up in the output when you run the test. 它会插入测试名称中,因此在您运行测试时会显示在输出中。

So you might have something like: 因此,您可能会遇到以下情况:

should redirect_to("the URL for student two") { student_url students(:two) }

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM