Today, while writing some unit tests, I encountered a challenge. The user story was that, when a Person's details are updated, the display should be updated to reflect the changes.
I'd implemented this feature using a signal on the person class that will be called whenever any details are updated:
This is a fairly standard application of an observer pattern that you might find in any MVC application.
But the question is, using the Boost unit test framework, how can I test if my signal has been called?
The mock signal handler
To test the signal handler, we'll use a functor as a mock signal handler, that sets an internal flag when it gets called. In the functor's destructor, we'll do a test on the flag to make sure it got set:
The test case
Once we've written a mock, the test case is pretty simple. Note that I wrap my handler with a boost::ref, so that it doesn't get copied.
This works great. And if we comment out the updated signal call in person::name():