I'm really getting to TDD recently, and after reading Kent Beck's book on Test Driven Development, I've still got many questions around test design in my mind.
One of the issues I'm currently having is the use of Mock objects. Take below a very simple that generates a report:
public string MakeFinancialReport()
{
return sys1.GetData() + sys2.GetData() + sys3.GetData();
}
The report must contain a header, a body, and a footer. So a quick test to see if those titles exist in the report:
public void TestReport()
{
string report = MakeFinancialReport();
Assert.IsTrue(report.Contains("[Title]") && report.Contains("[Body]") && report.Contains("[Footer]"));
}
To isolate the method, I guess I would be mocking away the sys1, sys2 and sys3 calls. Now, if they are all mocks, what have I got left to test? Also, when I do mock them, why must I have tell the mock objects that they are going to be expected to be called once and return X etc. Should it not just be a black box test and the MakeFinancialReport can make as many calls as it wants to build the report?
I'm getting confused with such a little problem, I'm not sure what I'm missing. I see Mocking as taking away testable code, and for most simple methods, what is left to test isn't helpful at all.
Martin, I think you should use mocks for sys1-3, but they only need to be simple enough to return a single character string each.
This means your test should look like:
public void TestReport()
{
// Setup mocks for sys1-3
string report = MakeFinancialReport();
Assert.IsTrue(report.equals("abc"));
}
This shows that MakeFinancialReport
has the properties that it calls GetData()
from sys1-3 and it concatenates the results in this particular order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With