0

コントローラー仕様内でカスタム rspec マッチャーを使用していますが、メッセージは常に空です。

仕様は次のようになります。

describe QuestionnaireController do
  matcher :redirect_to_sign_in_if_not_authenticated do |method|
    match do |controller| 
      self.send(method)
      response.should redirect_to new_user_session_path
    end
  end

  describe "GET index" do
    it { should redirect_to_sign_in_if_not_authenticated(get :index) }
  end
end

このテストを実行して失敗すると、次のようになります。

Failures:

  1) QuestionnaireController GET show 

ご覧のとおり、デフォルトの should メッセージがここにはありません。どうすれば表示されるようになりますか?

4

1 に答える 1

0

failure_message_for_shouldここで説明されているように、ブロックを使用できます: https://www.relishapp.com/rspec/rspec-expectations/v/2-4/docs/custom-matchers/define-matcher#overriding-the-failure-message-当然のこと

ただし、ここでいくつかの問題に遭遇する可能性があります。

  • get :index実際にgetメソッドを呼び出してから、コードが期待していないように見える戻り値をマッチャーに渡します。
  • should redirect_toカスタムマッチャー内で別のマッチャー ( ) を使用すると、エラーとバックトレースが台無しになる可能性があります。

代わりに共有の例を検討することをお勧めします: https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples

于 2013-03-29T15:13:51.830 に答える