Any rule that says there should be only 1 assertion ever is stupid.
Any rule that says there should be only 1 assertion ever is stupid.
The only reason I can really see to have more than one assertion would be to avoid having to run the setup/teardown multiple times. However, its usually a desirable goal to write code that require little setup/teardown to test anyways because that comes with other benefits. Again, it might not be practical or even possible, but that goes of almost all programming "rules"..
So you have one test that indicates that a log error is outut. then another that tests that the property X in the return from the error is what you expect. then another test to determine that propery Y in return is what you expect?
that to me is wasteful, unclear, bloated. About the only useful result I can see that is it allows bragging about how many tests a project has.
That's one way to get dogma-driven assertion roulette, as you will not know which particular error occurred.
assertThat(fooReturningOptional(), isPresentAndIs(4))
Or
assertThat(shape, hasAreaEqualTo(10))
Or
AssertThat(polygonList, hasNoIntersections())
Custom matchers can go off the deep end really easily. One of those cases of learn the principle, then learn when it does not apply
That being said, there can of course me other tradeoffs e.g. performance and even cases where simple test setups are downright impossible.
I find that reducing assertions per spec where I can a good guideline. E.g. combining expect(foo['a']).to eq(1) and expect(foo['b']).to eq(2) into expect(foo).to include('a' => 1, 'b' => 2) yields better error messages.
Can always refactor to 2 tests if the test setup changes and the assertions begin to differ or become too complex.