Edit:
It seems that in trying to provide some solutions to my own problem, I blurred the whole problem. So I'm modifying the question a 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 really doesn't matter what happens in these methods. The important thing is that I have several unit tests to cover the GetProtocolHeader method, covering all situations (returning the correct title, zero or exception), and now I am writing unit tests for GetProtocolHeaderValue .
If GetProtocolHeaderValue is dependent on an external dependency, I could mock it and inject it (I use Moq + NUnit). Then my unit test will just check the expectation that the external dependency has been called and the expected value is returned. An external dependency would be tested according to its own unit test, and I would do it, but how to proceed in this example, where the method is not an external dependency?
Clarification of the problem:
I believe that my test package for GetProtocolHeaderValue should check if GetProtocolHeader returns a header, null or exception. Therefore, the main question is whether to write tests where GetProtocolHeader will actually be executed (some tests will be duplicated because they will test the same code as the tests for GetProtocolHeader itself), or should I use the mocking approach described by @adrift and @Eric Nicholson, where I will not run the real GetProtoclHeader , but just configure mock to return a header, null or exception when calling this method?
source share