Shoulda / Minitest redirect_to {url_helper}无法正常工作

时间:2015-10-02 19:25:39

标签: ruby-on-rails minitest shoulda

使用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行。我假设它想要控制器/动作哈希?

以下行按预期工作:

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

......就像这样...

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

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

或者,我是否错过了关于这个帮助者的目的/工作原理的其他信息?

1 个答案:

答案 0 :(得分:1)

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

所以你可能有类似的东西:

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

希望这有帮助。