Using NUnit 2.5 in VS 2008, i'm not sure how to test that a function simply return;
s when some condition is set. Is there an Assert
method that serves my purpose, or is this not testable?
views:
90answers:
4What code is after the return
statement? Does it modify any data? I usually check if the data that is operated on after the statement is untouched.
To give you an example, if I traverse a tree recursively based on some condition over node's name, I'll explicitly check that
- nodes that should have been visited were visited, and,
- nodes that should not have been visited were not visited.
Well, presumably there's some observable difference between the function simply returning, and it doing something before returning. Test that observable difference.
If there isn't an observable difference between it just returning and it not doing so, then either you're dealing with a generically "hard to test" bit of code (e.g. caching) where the effect is a non-functional one, or you should ask yourself why you've got that code in the first place.
Sometimes you encounter this sort of thing when you have a function whose sole job is to take in one or more objects, delegate some calls, then return.
A way to test that is with Mocks - create a mock of the passed in object, check the mock to ensure that the expected calls were actually made (which is the purpose of your function).
If the expected calls weren't made prior to return, your test will fail, otherwise it will pass.
Beware though, this sort of test flirts dangerously close to knowledge of the implementation.