<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tales from the Evil Empire : MoQ</title><link>http://weblogs.asp.net/bleroy/archive/tags/MoQ/default.aspx</link><description>Tags: MoQ</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Mocking indexer getters with Moq</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/16/mocking-indexer-getters-with-moq.aspx</link><pubDate>Thu, 16 Jul 2009 23:42:17 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7148938</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7148938</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/16/mocking-indexer-getters-with-moq.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy" border="0" alt="(c) Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN2687_61E2AA99.jpg" width="244" height="184" /&gt; This is a follow-up on that other post: &lt;a href="http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx"&gt;Mocking indexer setters with Moq&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;So thanks to that post, we now know how to intercept the setting of a particular indexed property (in our example, an application variable) and set a local variable with the value that was set by the tested code.&lt;/p&gt;  &lt;p&gt;Now if you want the application to return that same value when queried by the tested code, you also need to mock the indexer getter.&lt;/p&gt;  &lt;p&gt;This operation is also not entirely trivial. Here’s how you do it: you do a SetupGet chained with a Returns with a lambda expression as the parameter:&lt;/p&gt;  &lt;pre class="code"&gt;mockHttpContext&lt;br /&gt;    .SetupGet(c =&amp;gt; c.Application[&lt;span style="color: #2b91af"&gt;&amp;quot;foo&lt;span style="color: #2b91af"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;])
    .Returns(() =&amp;gt; (&lt;span style="color: blue"&gt;object&lt;/span&gt;)map);&lt;/pre&gt;

&lt;p&gt;That last point about using a lambda is pretty important. If you just use map as the parameter for Returns, Moq will hold on to a reference to whatever object map contained &lt;em&gt;at the time the call to Returns is made&lt;/em&gt;. This might very well be null, if you started with the code in our previous setter example. If you use a lambda on the other hand, Moq will not hold on to the value of map but to the expression that returns the value of map. The execution is deferred. Now every time the tested code exercises that path, Moq will evaluate the expression, which will return the &lt;em&gt;current&lt;/em&gt; value of map.&lt;/p&gt;

&lt;p&gt;Also of interest, whereas we had to explicitly have an index parameter for the callback lambda in the case of the setter, Returns needs no such thing, and this example actually has little code that is specific to indexed properties. The trick about the lambda in the Returns in particular applies just as well to mocking getters for non-indexed properties.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7148938" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MoQ/default.aspx">MoQ</category></item><item><title>Mocking indexer setters with Moq</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx</link><pubDate>Mon, 15 Jun 2009 22:27:52 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7124027</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7124027</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2004" border="0" alt="(c) Bertrand Le Roy 2004" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN2906_5A7A1A17.jpg" width="244" height="184" /&gt; I quite like &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt; because it makes sense for me. Shamefully, I’ve always had some trouble understanding test code that was using mocks built with other frameworks. With &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt;, I can just grok it for some reason. It’s just super-clear to me. It doesn’t mean I have any idea how it really works but for now I’m just happy with the magic.&lt;/p&gt;  &lt;p&gt;Anyway, yesterday I wanted to check that a controller action was setting some Application variable (let’s not get into the debate on whether or not it should do that at all). Something like this:&lt;/p&gt;  &lt;pre class="code"&gt;application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now how do I enable the object to be set by the tested code? Well, that one is easy, I can use &lt;a href="http://www.clariusconsulting.net/labs/moq/html/87C20A5E.htm"&gt;SetupSet&lt;/a&gt; on indexers just fine:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mockHttpContext = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();
mockHttpContext.SetupSet(&lt;br /&gt;    c =&amp;gt; c.Application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;());&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This call tells the mock that it can accept to run code that attempts to set the “Foo” application variable with any object.&lt;/p&gt;

&lt;p&gt;But now, what if I want to get a reference to that object from my test code in order to check the value of some of its properties?&lt;/p&gt;

&lt;p&gt;Well, &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt; has a Callback method that you can hook to the result of any Setup call. The action that you provide it will be run whenever the setup code is called. The problem with that callback method is that its signature must match that of the setter exactly. Unfortunately, that signature is implicit. If you get it wrong, the test will fail more or less silently (it will just tell you setup failed with little details). To get this right, you need to know what the setter syntactic sugar compiles to, which kinda sucks, but the good news is that you only have to figure it out once, which I just spent some time doing for you (and for myself too, let’s be honest):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mockHttpContext = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();
&lt;span style="color: #2b91af"&gt;Foo &lt;/span&gt;map = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;br /&gt;mockHttpContext&lt;br /&gt;    .SetupSet(c =&amp;gt; c.Application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;())
    .Callback((&lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: blue"&gt;object &lt;/span&gt;m) =&amp;gt; { map = (&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;)m; });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Because this is an indexer setter, the compiled code actually takes a name and a value, which is reflected by the signature of the callback lambda. We can now call into the code to test, knowing that when it sets our “Foo” application variable, the local “map” variable of the test code will get set. The test code can then party on the object and assert whatever it wants.&lt;/p&gt;

&lt;p&gt;I hope this saves some time whoever is trying to do the same thing.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7124027" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MoQ/default.aspx">MoQ</category></item></channel></rss>