2016-03-23 8 views
1

rspec-mocks 'expect(target).to receive(:message).with(arg_matcher)は、withに渡されたargマッチャーと一致しないパラメーターでターゲットが呼び出された場合にのみ、テストの最後にエラーを表示します。ターゲットが不一致のパラメータで呼び出されるとすぐに、それを強制的に失敗させる方法がありますか? RRはこのように動作します。rspec-mocksのreceive.withが間違っていると期待する方法

私が直面している問題は、上記のようにarg_matcherでこのモックを設定すると、ターゲットが異なるパラメータで呼び出されているためテストが失敗しますが、テストの終了前に別のアサーションが失敗するためですこのアサーションからのエラーだけを見てください。期待されたパラメータと実際に呼び出されたものとの違いを私に見せてしまったはずのモックからではありません。

rspec-mocksの使用3.3.2。

答えて

2

私はしないでくださいreceiveを熱心に失敗させる方法を知っている。しかし、have_receivedは熱心に失敗するので、いくつかの期待の最初のものであれば、テストに失敗し、RSpecが報告します。

class Foo 
    def self.bar(baz) 
    end 
end 

describe "RSpec" do 
    it "reports a non-mock expectation failure before a mock expectation failure" do 
    expect(Foo).to receive(:bar).with(1) 
    expect(true).to be_falsy # RSpec reports this failure 
    Foo.bar 2 
    end 

    it "reports a spy expectation failure when you'd expect it to be reported" do 
    allow(Foo).to receive(:bar) # Spy on Foo 
    Foo.bar 2 
    expect(Foo).to have_received(:bar).with(1) # RSpec reports this failure 
    expect(true).to be_falsy 
    end 

end 

詳細はthe documentation of RSpec spiesを参照してください。

このソリューションであり、おそらく

  • 私は
  • を行うようにスパイ期待が失敗した場合は、論理的arrange-act-assert順番にテストを好む場合は、あなたが他の期待を気にしない場合は最高の
  • あなたが気にするよりもスパイを設定するallowの余分なタイピングを気にするならば、aggregate_failuresブロック

レギュラーモックsおよびADARSHのソリューションは、あなたがより少ないaggregate_failuresブロックを気にしている場合、あなたがスパイを設定allowの余分なタイピングを気よりも関わらず、どちらか

  • を失敗したかどうかの両方の期待の結果を見たい

    • 優れています
  • 1

    テストはターゲットが異なるのparamsと呼ばれているので、失敗を開始し、その後、別のアサーションはテストの終了前に失敗したので、私だけではない行方不明モック

    から、この主張からのエラーを参照してください

    これを正しく読んでいると、複数のアサーションが1つの仕様にあり、最初のものが失敗しているので、2番目または3番目のアサーションの失敗は見られませんか?

    もしそうなら、私は失敗を集めるRSpec's aggregate_failures introduced in v3.3を、使用することを検討したが、その後のアサーションを実行したい:

    aggregate_failures("verifying response") do 
        expect(response.status).to eq(200) 
        expect(response.headers).to include("Content-Type" => "text/plain") 
        expect(response.body).to include("Success") 
    end 
    
    # Which gives you nice errors for all assertions 
    
    1) Client returns a successful response 
        Got 3 failures: 
    
        1.1) Got 3 failures from failure aggregation block "testing response". 
          # ./spec/use_block_form_spec.rb:18 
          # ./spec/use_block_form_spec.rb:10 
    
          1.1.1) Failure/Error: expect(response.status).to eq(200) 
    
            expected: 200 
             got: 404 
    
            (compared using ==) 
           # ./spec/use_block_form_spec.rb:19 
    
          1.1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json") 
            expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"} 
            Diff: 
            @@ -1,2 +1,2 @@ 
            -[{"Content-Type"=>"application/json"}] 
            +"Content-Type" => "text/plain", 
           # ./spec/use_block_form_spec.rb:20 
    
          1.1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}') 
    
            expected: "{\"message\":\"Success\"}" 
             got: "Not Found" 
    
            (compared using ==) 
           # ./spec/use_block_form_spec.rb:21 
    
        1.2) Failure/Error: expect(false).to be(true), "after hook failure" 
          after hook failure 
          # ./spec/use_block_form_spec.rb:6 
          # ./spec/use_block_form_spec.rb:10 
    
        1.3) Failure/Error: expect(false).to be(true), "around hook failure" 
          around hook failure 
          # ./spec/use_block_form_spec.rb:12 
    

    あなたはまたaggregate_failuresに記述するブロックにタグを付けることができます。

    RSpec.describe ClassName, :aggregate_failures do 
        # stuff 
    end 
    
    +0

    これは素晴らしい情報です。ありがとうございます!しかし、私の問題は違っていました。@Dave Schweisguthは正しく理解しました。 – astgtciv

    +1

    Adarshはあなたの問題をかなり修正していませんでしたが、 'aggregate_failures'はそれを解決します:あなたのテスト全体を' aggregate_failures'にラップすると、非モック失敗とモック失敗の両方が表示されます。 (上で述べたように、ノンモックの失敗は、あなたがテストしているものによって決まりますか? –

    関連する問題