Hi guys,
Using the boost unit test framework, I'm trying to test a simple
scenario: when a person's name is set, their 'updated' signal is
triggered.
Here's my person class:
class person
{
public:
person() : name_() {};
void name(const std::string & name)
{
name_ = name;
updated(*this);
};
boost::signal updated;
private:
std::string name_;
};
To test this, I've created a mock handler for the updated event. In
it's destructor, it checks has been called (and also with the correct
subject):
struct mock_handler
{
mock_handler(const person & subject) :
has_been_called_(false), subject_(subject) {};
// Handler.
void operator()(const person & subject)
{
BOOST_TEST_MESSAGE("in mock_handler::operator()");
has_been_called_ = true;
BOOST_CHECK_EQUAL(&subject == &subject_, true);
};
// This handler must be called before it goes out of scope.
~mock_handler()
{
BOOST_TEST_MESSAGE("in mock_handler::~mock_handler()");
BOOST_CHECK_EQUAL(has_been_called_, true);
};
private:
bool has_been_called_;
const person & subject_;
};
And then writing my test case to use it:
BOOST_AUTO_TEST_CASE(setting_name_triggers_update_signal)
{
BOOST_TEST_MESSAGE("entered test");
person subject;
mock_handler handler(subject);
subject.updated.connect(handler);
subject.name("Richard");
BOOST_TEST_MESSAGE("exiting test");
}
Unfortunately, when this code runs, it produces the following output:
Running 1 test case...
entered test
in mock_handler::~mock_handler()
test.cpp(46): error in "setting_name_triggers_update_signal": check
has_been_called_ == true failed [0 != 1]
in mock_handler::~mock_handler()
test.cpp(46): error in "setting_name_triggers_update_signal": check
has_been_called_ == true failed [0 != 1]
in mock_handler::~mock_handler()
test.cpp(46): error in "setting_name_triggers_update_signal": check
has_been_called_ == true failed [0 != 1]
in mock_handler::operator()
exiting test
in mock_handler::~mock_handler()
test.cpp(46): error in "setting_name_triggers_update_signal": check
has_been_called_ == true failed [0 != 1]
in mock_handler::~mock_handler()
*** 4 failures detected in test suite "tests"
I'm not sure what's going on here, or why mock_handler's destructor is
being called 4 extra times.
Can anyone shed some light as to why this might be happening?
Cheers,
Rich