16 Dec 2011 11:44
how big unit tests should be
tomashkerr <tomashkerr <at> yahoo.com>
2011-12-16 10:44:50 GMT
2011-12-16 10:44:50 GMT
I'm writing some pet-project code to practice my TDD skills and have one doubt according to tests size and complexity. Let's say I need to implement a class that fetches data from external system (say DB), then formats specific response using this data and writes the response via external interface. I think of using mock objects here. I suppose I need db access service mock, response formatter mock and external interface mock. Now is it ok to write first test exercising all that stuff at once? I mean (pseudocode): testExecute(): // prepare expect(dbMock).toCall(fetch).andReturn(result) expect(formatterMock).toCall(format).with(result).andReturn(formattedResponse) expect(externalApiMock).toCall(send).with(formattedResponse) // act myComponent.execute() // exercise // nothing here Or should I write separate tests for each call or incrementally add mock calls to this one? Now suppose I need to log message and return specific empty response if something is wrong with data load. How should I name this test? I think of "testReturnEmptyResponseAndWriteLogOnException". But as the names contains "and" it seems that this test does too much. Again should I separate testing response and logging separately? I'd be grateful if you could point me to right direction here(Continue reading)
> The "state-based" approach can be:
>
> - outside-in, with a stack of partial work in progress as you work
> inwards
> - inside-out, where the leaf classes need to be done before those
> classes that use them. Hence my comment, George. And Sam, perhaps you're
> assuming this to be the only approach?
>
> I prefer to TDD in a GOOS style because it works better for me than in the
> past when I didn't use mocks. I like working outside-in and not having to
> carry partial work. The GOOS style encourages the definition of roles, and
> I'm keen for the reduced dependencies that result. Of course, it's possible
RSS Feed