簡體   English   中英

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測試是新手,我的代碼似乎與Minitest文檔的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) } 

第一個解決方案是完全多余的。 第二個似乎多余。 我意識到此方法的源沒有單個參數的默認值。 應該是?

或者,我是否明顯缺少有關此助手的目的/工作的其他信息?

這實際上是文檔中的錯誤,實際上是最近才提出來的一個問題: https : //github.com/thoughtbot/shoulda-matchers/issues/788 第一個參數是路線的人類可讀的表示形式; 它會插入測試名稱中,因此在您運行測試時會顯示在輸出中。

因此,您可能會遇到以下情況:

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM