<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Frozentux &#187; Unit test</title>
	<atom:link href="http://www.frozentux.net/tag/unit-test/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.frozentux.net</link>
	<description>Yet another site</description>
	<lastBuildDate>Thu, 13 Oct 2011 19:37:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Unit testing and stubbing singletons</title>
		<link>http://www.frozentux.net/2010/05/unit-testing-and-stubbing-singletons/</link>
		<comments>http://www.frozentux.net/2010/05/unit-testing-and-stubbing-singletons/#comments</comments>
		<pubDate>Tue, 04 May 2010 19:58:58 +0000</pubDate>
		<dc:creator>Oskar Andreasson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Stub]]></category>
		<category><![CDATA[Unit test]]></category>

		<guid isPermaLink="false">http://www.frozentux.net/?p=581</guid>
		<description><![CDATA[I got a bit curious about stubbing Singletons for testing during the weekend as well. We often find ourselves needing to test large codebases at work, and in the current project I&#8217;m in, we do complete end to end signal flow tests, but people are finally realizing that this will simply not do. For this [...]]]></description>
			<content:encoded><![CDATA[<p>I got a bit curious about stubbing Singletons for testing during the weekend as well. We often find ourselves  needing to test large codebases at work, and in the current project I&#8217;m in, we do complete end to end signal flow tests, but people are finally realizing that this will simply not do. For this reason, we&#8217;re doing a lot of work to try to split the entire project up into manageable chunks. One of the main problems has been the incessant use of singletons. A simply half-way-there to doing full out interfaces is to simply make all public function calls virtual and then create a stub class of the singleton saving the message or whatever passed on, into a variable which can be grabbed and tested from the actual unit test. </p>
<p>A sample of the general idea below:</p>
<pre name="code" class="c++:nogutter">
#include <iostream>

class A
{
public:
    static A *instance()
    {
        std::cout << "A::instance()" << std::endl;
        if (!s_instance)
            s_instance = new A;
        return s_instance;
    };

    A()
    {
        std::cout << "A::A()" << std::endl;
    };
    // Virtual makes the difference
    virtual void send(int i)
    {
        std::cout << "A::send()" << std::endl;
        // Blah blah, send i or something
    };
    static A *s_instance;
private:
};

class stub_A: public A
{
public:
    static stub_A *instance()
    {
        std::cout << "stub_A::instance()" << std::endl;
        if (!s_instance)
        {
            s_instance = new stub_A;
            A::s_instance = s_instance;
        }
        return s_instance;
    };

    stub_A()
    {
        std::cout << "stub_A::stub_A()" << std::endl;
    };

    void send(int i)
    {
        std::cout << "stub_A::send()" << std::endl;
        y = i;
    };

    int getMessage()
    {
        return y;
    };
private:
    int y;
    static stub_A *s_instance;
};

A *A::s_instance = 0;
stub_A *stub_A::s_instance = 0;

int main(int argc, char **argv)
{
    stub_A::instance()->send(5);
    std::cout << "stub_A::instance()->getMessage() == " <<
        stub_A::instance()->getMessage() << std::endl;

    A::instance()->send(7);
    std::cout << "stub_A::instance()->getMessage() == " <<
        stub_A::instance()->getMessage() << std::endl;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.frozentux.net/2010/05/unit-testing-and-stubbing-singletons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

