This is pretty basic question around unit testing.
I have a method e.g. GetOrderDetails which calls a Repository to fetch order details. I have a mock repository which can be setup to return stock responses.
For testing GetOrderDetails method, I’ll at least use the following cases –
Repository call fails
- with an error code
- with an Exception
Repository call succeeded
- returned zero result
- returned one result
- returned more than one result
Should I be writing a single Test method to test above scenarios or should it really be a seperate test method for each of the above scenario?
I believe, breaking it down into multiple test methods would at least provide the following benefits
1. higher isolation in case of test failures
2. amount of code within a test method is less
3. each test method will have a single repository setup responsibility e.g. either setup for no result, or setup for multiple results etc
Could you please help with your views around this?
I would write a separate unit test for each case. Basically every time you write an
ifor aswitchinside the method you are testing it implies different unit tests to handle each case. The reason for this is that the arrange part of your unit test will be different based on which route you would like your code to take (different mock expectations, property setups, …). Also I would recommend you to organize your unit test in the following three parts: