Edit:
It seems that by trying to provide some solutions to my own problem I blurred the whole problem. So I’m modifying the question little bit.
Suppose I have this class:
public class ProtocolMessage : IMessage
{
public IHeader GetProtocolHeader(string name)
{
// Do some logic here including returning null
// and throw exception in some cases
return header;
}
public string GetProtocolHeaderValue(string name)
{
IHeader header = GetProtocolHeader(name);
// Do some logic here including returning null
// and throw exception in some cases
return value;
}
}
It is actually not important what’s going on in these methods. The important is that I have multiple unit tests to cover GetProtocolHeader method covering all situations (returning correct header, null or exception) and now I’m writing unit tests for GetProtocolHeaderValue.
If GetProtocolHeaderValue would be dependent on external dependency I would be able to mock it and inject it (I’m using Moq + NUnit). Then my unit test would just test expectation that external dependency was called and returned expected value. The external dependency would be tested by its own unit test and I would be done but how to correctly proceed in this example where method is not external dependency?
Clarification of the problem:
I believe my test suite for GetProtocolHeaderValue must test situation where GetProtocolHeader returns header, null or exception. So the main question is: Should I write tests where GetProtocolHeader will be really executed (some tests will be duplicated because they will test same code as tests for GetProtocolHeader itself) or should I use mocking approach described by @adrift and @Eric Nicholson where I will not run real GetProtoclHeader but just configure mock to return header, null or exception when this method is called?
In the call to
GetProtocolHeaderValue, do you actually need to know whether or not it calledGetProtocolHeader?Surely it is enough to know that it is getting the correct value from the correct header. How it actually got it is irrelevant to the unit test.
You are testing units of functionality, the unit of functionality of
GetProtocolHeaderValueis whether it returns the expected value, given a header name.It is true that you may wish to guard against inappropriate caching or cross-contamination or fetching the value from a different header, but I don’t think that testing that it has called
GetProtocolHeaderis the best way to do this. You can infer that it somehow fetched the right header from the fact that it returned the expected value for the header.As long as you craft your tests and test data in such a way as to ensure that duplicate headers don’t mask errors, then all should be well.
EDIT for updated question:
If
GetProtocolHeaderworks quickly, reliably and is idempotent, then I still believe that there is no need to mock it. A shortfall in any of those three aspects is (IMO) the principal reason for mocking.If (as I suspect from the question title), the reason you wish to mock it is that the preamble required to set up an appropriate state to return a real value is too verbose, and you’d rather not repeat it across the two tests, why not do it in the setup phase?
One of the roles performed by good unit tests is documentation.
If someone wishes to know how to use your class, they can examine the tests, and possibly copy and alter the test code to fit their purpose. This becomes difficult if the real idiom of usage has been obscured by the creation and injection of mocks.
Mocks can obscure potential bugs.
Let’s say that
GetProtocolHeaderthrows an exception if name is empty. You create a mock accordingly, and ensure thatGetProtocolHeaderValuehandles that exception appropriately. Later, you decide thatGetProtocolHeadershould returnnullfor an empty name. If you forget to update your mock,GetProtocolHeaderValue("")will now behave differently in real life vs. the test suite.Mocking might present an advantage if the mock is less verbose than the setup, but give the above points due consideration first.
Though you give three different
GetProtocolHeaderresponses (header, null or exception) thatGetProtocolHeaderValueneeds to test, I imagine that the first one is likely to be “a range of headers”. (e.g. What does it do with a header that is present, but empty? How does it treat leading and trailing whitespace? What about non-ASCII chars? Numbers?). If the setup for all of these is exceptionally verbose, it might be better to mock.