6

I've introduced visitors as one of core architecture ideas in one of my apps. I have several visitors that operate on a same stuff. Now, how should I test it? Some tests I'm thinking of are a bit larger then a unit test should be (integration test? whatever) but I still wanna do it. How would you test code like the C++ sample from wiki art on Visitor Pattern

bjb568
  • 11,089
  • 11
  • 50
  • 71
Nazgob
  • 8,502
  • 4
  • 40
  • 42

3 Answers3

4

Unit testing isn't really about testing patterns, it is about testing the correct implementation of methods and functions. The visitor pattern is a specific class structure, and for each of the classes (ConcreteVisitor and ConcreteElement, specifically) involved you'll want unit tests.

When you've developed confidence that your class methods are behaving OK, you could use your unit test framework to develop integration tests as well. Do not start integration testing rightaway: you'll find that you develop a lot of integration tests that are actually testing the behavior of a specific class, i.e. unit tests.

Whether you need mock objects or can use 'real' objects is a different matter. This depends a lot on whether the objects behave nice enough for unit test purposes (i.e. they do not pull in a lot of additional dependencies etc.), and whether the objects themselves are unit tested (i.e. you need to be able to trust these objects 100%). The mock vs. real objects issue has been addressed on stackflow before, so search the unittest tags.

andreas buykx
  • 12,608
  • 10
  • 62
  • 76
2

make a test visitor object and make it visit things.... test that it visited the right things.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • Can you give a simple example as I am using Jsqlparser which uses visitor pattern and wanted to know how to test them. – MSS Jan 25 '17 at 13:13
  • you probably want to ask your own question. show some basic code and what you are having trouble with testing. I don't do C++ any more, but someone else will likely give you code showing how. – Keith Nicholas Jan 25 '17 at 21:59
  • Here is the [link](http://stackoverflow.com/questions/41887920/how-to-do-unit-testing-of-visitors-in-jsqlparser) to my question. – MSS Jan 27 '17 at 06:14
2

You can create mock objects and have your visitors visit them, and then create mock visitors, and test that the right actions were performed.

pkaeding
  • 36,513
  • 30
  • 103
  • 141