<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Moq on Bits and Pieces</title>
		<link>https://bits.strawjackal.org/tags/moq/</link>
		<description>Recent content in Moq on Bits and Pieces</description>
		<generator>Hugo</generator>
		<language>en-us</language>
		
		
		
		
			<lastBuildDate>Fri, 07 Oct 2011 00:16:03 +0200</lastBuildDate>
		
			<atom:link href="https://bits.strawjackal.org/tags/moq/index.xml" rel="self" type="application/rss+xml" />
			<item>
				<title>Moq, Callbacks and Out parameters: a particularly tricky edge case</title>
				<link>https://bits.strawjackal.org/113/</link>
				<pubDate>Fri, 07 Oct 2011 00:16:03 +0200</pubDate>
				<guid>https://bits.strawjackal.org/113/</guid>
				<description>&lt;p&gt;In my current project we’re using &lt;a href=&#34;http://code.google.com/p/moq&#34;&gt;Moq&lt;/a&gt; as our mocking framework for unit testing. It’s a very nice package – very simple, very intuitive for someone who has his wrapped around basic mocking concepts, but tonight I ran into an annoying limitation that, while rare, impeded my tests considerably.&lt;/p&gt;  &lt;p&gt;Consider the following code:&lt;/p&gt;  &lt;pre style=&#34;border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 567px; padding-right: 5px; height: 390px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px&#34;&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  1: &lt;span style=&#34;color: #0000ff&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #0000ff&#34;&gt;interface&lt;/span&gt; IOut&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  2: {&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  3:    &lt;span style=&#34;color: #0000ff&#34;&gt;void&lt;/span&gt; DoOut (&lt;span style=&#34;color: #0000ff&#34;&gt;out&lt;/span&gt; &lt;span style=&#34;color: #0000ff&#34;&gt;string&lt;/span&gt; outval);&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  4: }&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  5: &#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  6: [Test]&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  7: &lt;span style=&#34;color: #0000ff&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #0000ff&#34;&gt;void&lt;/span&gt; TestMethod()&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  8: {&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt;  9:   var mock = &lt;span style=&#34;color: #0000ff&#34;&gt;new&lt;/span&gt; Mock&lt;IOut&gt;();&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt; 10:   &lt;span style=&#34;color: #0000ff&#34;&gt;string&lt;/span&gt; outVal = &#34;&lt;span style=&#34;color: #8b0000&#34;&gt;&lt;/span&gt;&#34;;&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt; 11:   mock.Setup(&lt;span style=&#34;color: #0000ff&#34;&gt;out&lt;/span&gt; =&gt; &lt;span style=&#34;color: #0000ff&#34;&gt;out&lt;/span&gt;.DoOut(&lt;span style=&#34;color: #0000ff&#34;&gt;out&lt;/span&gt; outVal))&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #ffff00; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt; 12:       .Callback&lt;&lt;span style=&#34;color: #0000ff&#34;&gt;string&lt;/span&gt;&gt;(theValue =&gt; DoSomethingWithTheValue(theValue));&#xD;&#xA;&lt;/pre&gt;&lt;pre style=&#34;background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px&#34;&gt; 13: }&lt;/pre&gt;&lt;/pre&gt;&#xD;&#xA;&lt;p&gt;What this code SHOULD do is create a mock object for the IOut interface that, when called with an out parameter, both assigns a value to it, and does something with the value in the Callback. &lt;/p&gt;</description>
			</item>
	</channel>
</rss>
