3 ms·
This is correct - multiple asserts are OK, but there are still good guidelines: A good unit test has the phases Arrange, Act, Assert, end of unit test. You ca
by SideburnsOfDoom 4y ago
This is correct - multiple asserts are OK, but there are still good guidelines:
A good unit test has the phases Arrange, Act, Assert, end of unit test.
You can use multiple assert statements in the "assert" phase, to check the specifics of the single logical outcome of the test.
In fact, once I see the same group of asserts used 3 or more times, I usually extract a helper method, e.g. "AssertCacheIsPopulated" or "AssertHttpResponseIsSuccessContainingOrder" these might have method bodies that contain multiple assert statements, but the question of is this a "single assert" or not, is a matter of perspective and not all that important.
The thing to look out for is - does the test both assert that e.g. the response is an order, and that the cache is populated? Those should likely be separate tests as they are logically distinct outcomes.
The test ends after the asserts - You do not follow up the asserts with a second action. That should be a different test.