c# - How to use TDD when your method is designed to perform many tasks -
i'm still trying follow path tdd.
let's have sunsystembroker triggered when file uploaded shared folder. broker designed open file, extract records it, try find associated payments in systems , call workflow!
if want follow tdd develop ibroker.process() method how shall do?
note: broker independent assemblies inheriting ibroker , loaded console app (like plugins).
this console in charge of triggering each broker!
public interface ifiletriggeredbroker : ibroker { filesystemtrigger trigger { get; } void process(string file); } public class sunsystempaymentbroker : ifiletriggeredbroker { private readonly idbdatasourcefactory _hrdbfactory; private readonly iexceldatasourcefactory _xlfactory; private readonly ik2datasourcefactory _k2factory; private ilog _log; public void process(string file) { (...) // _xlfactory.create(file) > extract // _hrdbfactory.create() > find // compare records // _k2factory.create > start } }
each method tested individually.
thank seb
given each method:
_xlfactory.create(file); _hrdbfactory.create(); // compare records _k2factory.create();
is tested individually, there little logic test within process(file)
.
if use moq, can check calls occur:
// arrange const string file = "file.xlsx"; var xlfactory = new mock<iexceldatasourcefactory>(); var hrbdfactory = new mock<idbdatasourcefactory>(); var k2factory = new mock<ik2datasourcefactory>(); // act var sut = new sunsystempaymentbroker(xlfactory.object, hrdbfactory.object, k2factory.object); // i'm assuming you're using constructor injection sut.processfile(file); // assert xlfactory.verify(m => m.create(file), times.once); hrbdfactory.verify(m => m.create(), times.once); k2factory.verify(m => m.create(), times.once);
for brevity, i've done single test, breaking 3 tests single "assert" (the verify
calls) more realistic. tdd write each test, before wiring method within process(file)
.
you may want @ having larger, integration level tests, pass in concrete versions of iexceldatasourcefactory
, ik2datasourcefactory
, idbdatasourcefactory
, exercise system in more depth.
in book growing object-oriented software guided tests, defined acceptance test written before work began, , failing whilst feature added in smaller tdd loops of functionality, work toward overall feature.