Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TDD Mocking - Is specifying mock object behaviour white box testing?

Tags:

tdd

mocking

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.

like image 891
Martin Avatar asked Feb 18 '10 23:02

Martin


1 Answers

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.

like image 109
quamrana Avatar answered Jun 28 '23 17:06

quamrana