Shoulda/Minitest redirect_to { url_helper } 没有哈希就不能工作

Shoulda/Minitest redirect_to { url_helper } not working without hash

使用 Rails 4.2、Minitest 和 Shoulda...

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

Shoulda 的新手和 Rails 一般测试,我的代码似乎与 the Minitest documentation for redirect_to 并行。但是,当 运行 测试时我遇到了这个错误...

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

...指向 should redirect_to 行。我假设它需要 controller/action 散列?

以下行按预期工作:

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

...就像这样...

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

第一个修复完全是多余的。第二个似乎是多余的。我意识到 the source for this method 没有单个参数的默认值。应该吗?

或者,我是否遗漏了关于此助手的 purpose/workings 的其他明显信息?

这实际上是文档中的错误,事实上最近才作为问题提出:https://github.com/thoughtbot/shoulda-matchers/issues/788。第一个参数是路线的人类可读表示;它被插入到测试的名称中,因此当您 运行 测试时将显示在输出中。

所以你可能有这样的东西:

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

希望对您有所帮助。