Adrián Pérez
Adrián Pérez

Reputation: 2216

Should static factories be tested when doing TDD?

I know that in TDD, you should avoid writing code that is not tested beforehand, but I'm wondering if this is one of those times where the rule might be broken for the sake of simplicity (avoiding testing of wrappers for third party libraries for testing purposes, like the gateway pattern for instance). I'm currently testing them though, something like this:

[TestMethod]
public void CreateShouldReturnANewInstance()
{
        var tcpClientMock = new Mock<ITcpClient>();
        var spooler2 = OutboundMessageSpooler.Create(tcpClientMock.Object);
        Assert.IsTrue(spooler2 != null && spooler2 is OutboundMessageSpooler);
}

Upvotes: 0

Views: 95

Answers (1)

Carl Manaster
Carl Manaster

Reputation: 40336

The test you've written strikes me as a good test, but not a very important one. There is this bit of functionality which creates a spooler from a TCP client, and this test demonstrates that that unit of functionality works. That's a good way to drive that bit of functionality - it didn't work before, you demonstrated with a red test, you wrote the method, and now it works. Cool. Necessary? Probably not. Helpful for regression? Again, probably not. Helpful as a model for tests of future, perhaps more complex, logic? Maybe.

I wouldn't advise you against writing tests like these - after all, they're pretty easy to write and may help you think about how you want the feature implemented - what TDD is really all about - but I probably wouldn't urge you to write them, either. See how helpful you find it, and if the answer is "not much," spend your test-driving resources elsewhere.

Upvotes: 1

Related Questions