3 ms·
> I’ve seen people take a dogmatic approach to this in Ruby without really applying any critical thought, because one assertion per test means your test is ‘cle
by OJFord 4y ago
> I’ve seen people take a dogmatic approach to this in Ruby without really applying any critical thought, because one assertion per test means your test is ‘clean’.
I can't speak for Ruby, but what I would call 'clean' and happily dogmatise is that assertions should come at the end, after setup and exercise.
I don't care how many there are, but they come last. I really hate tests that look like:
setup()
exercise(but_not_for_the_last_time)
assert state.currently = blah
state.do_other()
something(state)
assert state.now = blegh
And so on. It stinks of an integration test forced into a unit testing framework.
I like them to look like:
foo = FooFactory(prop="whatever")
result = do(foo)
assert result == "bar"
I.e. some setup, something clearly under test, and then the assertion(s) checking the result.
- ljm 4y agoI think even with integration tests they should still be treated similarly - at the end of the day you are setting expectations on an output given a certain input, there’s just a lot more going on in between. There’s no avoiding it though when you want something end-to-end, or a synthetic test. You’re piling up a whole succession of stateful actions and if you tested them in isolation you would fail to capture bugs that depend on state. In that sense, better to run a ‘signup, authenticate and onboard’ flow in one test instead of breaking it down.
- int_19h 4y agoHere's a trivial rewrite that satisfies your dogmatic requirement without any meaningful difference: setup() exercise(but_not_for_the_last_time) was_blah = (state.currently == blah) state.do_other() something(state) assert was_blah && state.now == blegh In fact, this last version is worse, because if do_other() can fail if state wasn't blah, then what you'll get is the exception from that failure interrupting the test before the assert would have been reported.
- OJFord 4y agoExactly because that's 'without any meaningful difference' is why I don't like that either. I'm not obsessing over purely the 'assert' keyword as you perhaps think I am, it's the structure I don't like.