Slipping a Fake
Templatize Collaborator Type Example
The trouble method in the following example has two awkward collaborators. We've already
taken care of one, the output stream, with the Parameterize Method technique: the method
receives an output stream object rather than assume one.However, the other collaborator is the database, and we can't fake it. Instead, we templatize the type of the database:
template<typename DBType>
void printAllOrders(ostream& outStream)
{
DBType db(ORDER_PROCESSOR_DB);
QueryResult qRes = db.get(ALL_ORDERS_QUERY);
outStream << qRes.columnNames();
for (int i = 0; i < qRes.rows(); i++)
outStream << qRes.rowData(i);
}
Production code calls the templatized method with a production database type:
void formatAndPrintReport()
{
printAllOrders<AppDB>(cout);
}
Our test calls the templatized method with a fake database type:
TEST(allOrdersReports)
{
stringstream oStream;
printAllOrders<FakeDB>(oStream);
ASSERT_EQ(EXPECTED_OUTPUT, oStream.str());
}
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |