<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>TypeMock</title>
        <link>http://ryanrivest.com/blog/category/3.aspx</link>
        <description>TypeMock</description>
        <language>en-US</language>
        <copyright>Ryan Rivest</copyright>
        <generator>Subtext Version 2.1.1.1</generator>
        <item>
            <title>Tip: Returning Multiple Mocks with the Same Method</title>
            <link>http://ryanrivest.com/blog/archive/2009/07/11/tip-returning-multiple-mocks-with-the-same-method.aspx</link>
            <description>&lt;p&gt;Being fairly new to unit testing, I find myself discovering all kinds of interesting challenges when trying to test my data access layer (DAL).  I had decided early on in one of my projects (with the tutelage of my supervisor and mentor) to take on the task of writing my DAL from scratch.  &lt;/p&gt;  &lt;p&gt;&lt;em&gt;Disclaimer:  I realize that data access is a problem that has already been solved (many times over), this was more of a learning exercise for me.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Part of my DAL deals with the IDbCommand.CreateParameter() method.  I ran into a problem where I wanted each subsequent call of CreateParameter() to return a different mock IDbDataParameter.  After some brief searching, I ran into a thread on &lt;a href="http://groups.google.com/group/moqdisc/browse_thread/thread/f1fb3894b9d43625/149686320e89d51b?lnk=raot"&gt;Moq Discussions&lt;/a&gt; from someone with the same problem, and a very elegant solution.&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;var parameters = new Queue&amp;lt;IDbDataParameter&amp;gt;(new[] { mockFirstParam.Object, mockSecondParam.Object }); 

mockCommand.Expect(c =&amp;gt; c.CreateParameter()).Returns(() =&amp;gt; pq.Dequeue());&lt;/pre&gt;

&lt;p&gt;First we set up a generic Queue of IDbDataParameters, initialized with an array of mock IDbDataParameter objects that have already been created. &lt;/p&gt;

&lt;p&gt;Then we set the behavior of our mock IDbCommand’s CreateParameter() method to the queue's Dequeue() method so that each subsequent call will return the next mock in the queue.&lt;/p&gt;

&lt;p&gt;We use TypeMock Isolator in our shop, so here's the same code using TypeMock Isolator's AAA syntax:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;var parameters = new Queue&amp;lt;IDbDataParameter&amp;gt;(new[] { mockFirstParam, mockSecondParam });

Isolate.WhenCalled(() =&amp;gt; command.CreateParameter()).DoInstead(param =&amp;gt; parameters.Dequeue());&lt;/pre&gt;

&lt;p&gt;This code behaves the same as its Moq counterpart, the only difference is how we use the API.  &lt;/p&gt;

&lt;p&gt;Hope someone else finds this useful.  Thanks to the original author!&lt;/p&gt;&lt;img src="http://ryanrivest.com/blog/aggbug/7.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ryan Rivest</dc:creator>
            <guid>http://ryanrivest.com/blog/archive/2009/07/11/tip-returning-multiple-mocks-with-the-same-method.aspx</guid>
            <pubDate>Sat, 11 Jul 2009 07:13:18 GMT</pubDate>
            <wfw:comment>http://ryanrivest.com/blog/comments/7.aspx</wfw:comment>
            <comments>http://ryanrivest.com/blog/archive/2009/07/11/tip-returning-multiple-mocks-with-the-same-method.aspx#feedback</comments>
            <wfw:commentRss>http://ryanrivest.com/blog/comments/commentRss/7.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>