简体   繁体   中英

Shoulda/Minitest redirect_to { url_helper } not working without hash

Using Rails 4.2, Minitest, and 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

New to Shoulda and Rails testing in general, and my code seems to parallel the Minitest documentation for 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. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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