Unit-testing – Unit testing code paths

code smellunit testing

When unit testing using expectations, you define a set of method calls and corresponding results for those calls. These define the path through the method that you want to test.

I have read that unit tests should not duplicate the code. But when you define these expectations, isn't that duplicating the code, or at least the process? How do you know when you're duplicating functionality under test?

Best Answer

Expectations should be different but if you're testing with a common set of expectations across several unit tests you should use variables and objects that are shared.

This is also why you have the setUp and tearDown methods for in xUnit frameworks.

Some frameworks take this one step further letting you set up with nesting such as Jasmine (BDD/TDD for Javascript).