Are multiple asserts bad in a unit test? Even if chaining?

后端 未结 6 1387
借酒劲吻你
借酒劲吻你 2020-12-24 01:23

Is there anything wrong with checking so many things in this unit test?:

ActualModel = ActualResult.AssertViewRendered()        // check 1
                           


        
6条回答
  •  一整个雨季
    2020-12-24 02:10

    It's best to stick with only one assert in each test to avoid Assertion Roulette.

    If you need to set up the same scenario to test multiple conditions based on identical premises, it's better to extract the setup code to a shared helper method, and then write several tests that call this helper method.

    This ensures that each test case tests only one thing.

    As always, there are exceptions to this rule, but since you are new to unit testing, I would recommend that you stick with the one assertion per unit test rule until you have learned when it's okay to deviate.

提交回复
热议问题