lslah
lslah

Reputation: 556

How to unit test a parser which returns 'tell-dont-ask' objects?

I have two classes, Parser and Item. The Parser class parses some structured document and returns Item-objects if you call something like Parser::GetItem(int some_id). The Item class was written with the "Tell - don't ask" principle in mind. By that I mean it doesn't have getter-methods for several internal variables which were filled by the Parser on construction.

The question now is: How can I unittest the Parser class? How to check if the internal Item variables were correctly parsed? Do I have to rearrange my classes? Is it maybe bad design that the parser-interface returns fully constructed Item objects?

Upvotes: 0

Views: 136

Answers (1)

Augusto
Augusto

Reputation: 29927

Your design might not be ideal, but it's difficult to know without looking at the code.

Something to ask yourself is the Parser hiding too much information, and as a consequence doing too much.

I strongly suggest you to watch this presentation from Michael Feather, in which (interestingly) he discusses how to improve the design of a parser using tests, and it looks like the problem he solves is similar to what you're trying to solve. Michael Feathers - the deep synergy between testability and good design

Upvotes: 1

Related Questions