<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Boost: How do I write a unit test for a signal?</title>
	<atom:link href="http://richarddingwall.name/2008/06/08/boost-how-do-i-write-a-unit-test-for-a-signal/feed/" rel="self" type="application/rss+xml" />
	<link>http://richarddingwall.name/2008/06/08/boost-how-do-i-write-a-unit-test-for-a-signal/</link>
	<description>The adventures of a young kiwi software developer in London</description>
	<lastBuildDate>Tue, 31 Jan 2012 19:56:07 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: sheepsimulator</title>
		<link>http://richarddingwall.name/2008/06/08/boost-how-do-i-write-a-unit-test-for-a-signal/comment-page-1/#comment-15027</link>
		<dc:creator>sheepsimulator</dc:creator>
		<pubDate>Mon, 21 Jun 2010 01:43:47 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=87#comment-15027</guid>
		<description>One of my coworkers made the following suggestion in code review using this method.  As written, the handler tests your code as a _side effect_, which assumes you know what handler is doing.  It might be better to be explicit in your tests about what you expect to happen in the handler.  You could do this:

struct mock_handler
{
    mock_handler() :
        has_been_called_(false)
    {};
 
    // The signal handler function.
    void operator()(const person &amp; person)
    {
        has_been_called_ = true;
        received_person_ = person;
    };

    bool has_been_called()
    {
        return has_been_called_;
    }

    bool person_received()
    {
        return actual_person;
    }
 
    // This handler must be called before it goes out of scope.
    ~mock_handler()
    {
    };
 
private:
    bool has_been_called_;
    person&amp; received_person_;
};

And then have:

// Test that setting a new name triggers the person.updated signal.
BOOST_AUTO_TEST_CASE(setting_name_triggers_update_signal)
{
    person subject;
    mock_handler handler(subject);
 
    subject.updated.connect(boost::ref(handler));
 
    // Change the person&#039;s name, triggering the updated signal.
    subject.name(&quot;Richard&quot;);

    BOOST_CHECK_EQUAL(handler.has_been_called(), true);
    BOOST_CHECK_EQUAL(handler.person_received(), subject);
}

This makes it more explicit as to what the handler is expecting to get, and may be easier to read/maintain.</description>
		<content:encoded><![CDATA[<p>One of my coworkers made the following suggestion in code review using this method.  As written, the handler tests your code as a _side effect_, which assumes you know what handler is doing.  It might be better to be explicit in your tests about what you expect to happen in the handler.  You could do this:</p>
<p>struct mock_handler<br />
{<br />
    mock_handler() :<br />
        has_been_called_(false)<br />
    {};</p>
<p>    // The signal handler function.<br />
    void operator()(const person &amp; person)<br />
    {<br />
        has_been_called_ = true;<br />
        received_person_ = person;<br />
    };</p>
<p>    bool has_been_called()<br />
    {<br />
        return has_been_called_;<br />
    }</p>
<p>    bool person_received()<br />
    {<br />
        return actual_person;<br />
    }</p>
<p>    // This handler must be called before it goes out of scope.<br />
    ~mock_handler()<br />
    {<br />
    };</p>
<p>private:<br />
    bool has_been_called_;<br />
    person&amp; received_person_;<br />
};</p>
<p>And then have:</p>
<p>// Test that setting a new name triggers the person.updated signal.<br />
BOOST_AUTO_TEST_CASE(setting_name_triggers_update_signal)<br />
{<br />
    person subject;<br />
    mock_handler handler(subject);</p>
<p>    subject.updated.connect(boost::ref(handler));</p>
<p>    // Change the person&#8217;s name, triggering the updated signal.<br />
    subject.name(&#8220;Richard&#8221;);</p>
<p>    BOOST_CHECK_EQUAL(handler.has_been_called(), true);<br />
    BOOST_CHECK_EQUAL(handler.person_received(), subject);<br />
}</p>
<p>This makes it more explicit as to what the handler is expecting to get, and may be easier to read/maintain.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sheepsimulator</title>
		<link>http://richarddingwall.name/2008/06/08/boost-how-do-i-write-a-unit-test-for-a-signal/comment-page-1/#comment-14514</link>
		<dc:creator>sheepsimulator</dc:creator>
		<pubDate>Sat, 15 May 2010 20:48:16 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=87#comment-14514</guid>
		<description>Note:  if you use this idiom to write a checker method, be careful about putting a unit-test-system assert inside the destructor of a mock class if it throws a C++ exception.  If the assert fails in operator() *and* the assert fails in the destructor, you&#039;ll only see the destructor&#039;s assert and not the one from operator().</description>
		<content:encoded><![CDATA[<p>Note:  if you use this idiom to write a checker method, be careful about putting a unit-test-system assert inside the destructor of a mock class if it throws a C++ exception.  If the assert fails in operator() *and* the assert fails in the destructor, you&#8217;ll only see the destructor&#8217;s assert and not the one from operator().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sheepsimulator</title>
		<link>http://richarddingwall.name/2008/06/08/boost-how-do-i-write-a-unit-test-for-a-signal/comment-page-1/#comment-14102</link>
		<dc:creator>sheepsimulator</dc:creator>
		<pubDate>Wed, 28 Apr 2010 20:42:13 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=87#comment-14102</guid>
		<description>Brilliant!  This is a great article for people who are learning how to do unit tests!  Thanks!</description>
		<content:encoded><![CDATA[<p>Brilliant!  This is a great article for people who are learning how to do unit tests!  Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

