<?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>Matthew Manela&#039;s Home Page &#187; ASP .NET MVC</title>
	<atom:link href="http://matthewmanela.com/category/asp-net-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewmanela.com</link>
	<description>The life and work of Matthew Manela</description>
	<lastBuildDate>Sun, 25 Jul 2010 18:00:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Useful Moq Extension Method</title>
		<link>http://matthewmanela.com/2009/11/24/useful-moq-extension-method/</link>
		<comments>http://matthewmanela.com/2009/11/24/useful-moq-extension-method/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 00:03:00 +0000</pubDate>
		<dc:creator>MattManela</dc:creator>
				<category><![CDATA[ASP .NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Moq]]></category>

		<guid isPermaLink="false">http://blogs.msdn.com/matt/archive/2009/11/24/useful-moq-extension-method.aspx</guid>
		<description><![CDATA[I have been working with ASP .NET MVC and I use the Moq mocking library to help test the code I write.&#160;&#160; Often in ASP MVC anonymous objects are passed around as function arguments.&#160; This is especially common in calls to RouteUrl.&#160; S...]]></description>
			<content:encoded><![CDATA[<P>I have been working with ASP .NET MVC and I use the <A href="http://code.google.com/p/moq/" mce_href="http://code.google.com/p/moq/">Moq</A> mocking library to help test the code I write.&nbsp;&nbsp; Often in ASP MVC anonymous objects are passed around as function arguments.&nbsp; This is especially common in calls to RouteUrl.&nbsp; Since I want to be able to test this and verify that it is called correctly I wrote a handy extension method to make it easier called <STRONG>AsMatch</STRONG>.</P>
<P>To be able to test the UrlHelper (since it doesn't have an interface or virtual methods) I wrote a simple wrapper around it with an interface to enable testing.&nbsp; Using that with this extension method makes testing route generation a breeze.</P>
<P>Given a call to generate a Url like this:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">var url = Url.RouteUrl("<SPAN style="COLOR: #8b0000">Show</SPAN>", <SPAN style="COLOR: #0000ff">new</SPAN> { projectName="<SPAN style="COLOR: #8b0000">matt</SPAN>" });</PRE></PRE>
<P>You can write a setup on your Mock of the Url helper using a similar syntax:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">UrlHelperMock
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">.Setup(x =&gt; x.RouteUrl("<SPAN style="COLOR: #8b0000">Show</SPAN>",<SPAN style="COLOR: #0000ff">new</SPAN>{ projectName="<SPAN style="COLOR: #8b0000">matt</SPAN>"}.AsMatch()))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">.Return("<SPAN style="COLOR: #8b0000">someUrlToTest</SPAN>");</PRE></PRE>
<P>Here is the code that defines the AsMatch extension method:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> ObjectMoqExtensions
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN> AsMatch(<SPAN style="COLOR: #0000ff">this</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN> @<SPAN style="COLOR: #0000ff">object</SPAN>)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> Match&lt;<SPAN style="COLOR: #0000ff">object</SPAN>&gt;.Create(testObject =&gt; DoObjectsMatch(@<SPAN style="COLOR: #0000ff">object</SPAN>, testObject));
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">bool</SPAN> DoObjectsMatch(<SPAN style="COLOR: #0000ff">object</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN>1, <SPAN style="COLOR: #0000ff">object</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN>2)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var props1 = ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN>1);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var props2 = ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN>2);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var query = from prop1 <SPAN style="COLOR: #0000ff">in</SPAN> props1
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                        join prop2 <SPAN style="COLOR: #0000ff">in</SPAN> props2 on prop1.Key equals prop2.Key
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                        select prop1.Value.Equals(prop2.Value);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> query.Count(x =&gt; x) == Math.Max(props1.Count(), props2.Count());
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> Dictionary&lt;<SPAN style="COLOR: #0000ff">string</SPAN>, <SPAN style="COLOR: #0000ff">object</SPAN>&gt; ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN> @<SPAN style="COLOR: #0000ff">object</SPAN>)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var dictionary = <SPAN style="COLOR: #0000ff">new</SPAN> Dictionary&lt;<SPAN style="COLOR: #0000ff">string</SPAN>, <SPAN style="COLOR: #0000ff">object</SPAN>&gt;();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var properties = TypeDescriptor.GetProperties(@<SPAN style="COLOR: #0000ff">object</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">foreach</SPAN> (PropertyDescriptor property <SPAN style="COLOR: #0000ff">in</SPAN> properties)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                dictionary.Add(property.Name, property.GetValue(@<SPAN style="COLOR: #0000ff">object</SPAN>));
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> dictionary;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }</PRE></PRE><img src="http://blogs.msdn.com/aggbug.aspx?PostID=9928293" width="1" height="1">]]></content:encoded>
			<wfw:commentRss>http://matthewmanela.com/2009/11/24/useful-moq-extension-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful Moq Extension Method</title>
		<link>http://matthewmanela.com/2009/11/24/useful-moq-extension-method-2/</link>
		<comments>http://matthewmanela.com/2009/11/24/useful-moq-extension-method-2/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 00:03:00 +0000</pubDate>
		<dc:creator>MattManela</dc:creator>
				<category><![CDATA[ASP .NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Moq]]></category>

		<guid isPermaLink="false">/b/matt/archive/2009/11/24/useful-moq-extension-method.aspx</guid>
		<description><![CDATA[I have been working with ASP .NET MVC and I use the Moq mocking library to help test the code I write.&#160;&#160; Often in ASP MVC anonymous objects are passed around as function arguments.&#160; This is especially common in calls to RouteUrl.&#160; S...]]></description>
			<content:encoded><![CDATA[<P>I have been working with ASP .NET MVC and I use the <A href="http://code.google.com/p/moq/" mce_href="http://code.google.com/p/moq/">Moq</A> mocking library to help test the code I write.&nbsp;&nbsp; Often in ASP MVC anonymous objects are passed around as function arguments.&nbsp; This is especially common in calls to RouteUrl.&nbsp; Since I want to be able to test this and verify that it is called correctly I wrote a handy extension method to make it easier called <STRONG>AsMatch</STRONG>.</P>
<P>To be able to test the UrlHelper (since it doesn't have an interface or virtual methods) I wrote a simple wrapper around it with an interface to enable testing.&nbsp; Using that with this extension method makes testing route generation a breeze.</P>
<P>Given a call to generate a Url like this:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">var url = Url.RouteUrl("<SPAN style="COLOR: #8b0000">Show</SPAN>", <SPAN style="COLOR: #0000ff">new</SPAN> { projectName="<SPAN style="COLOR: #8b0000">matt</SPAN>" });</PRE></PRE>
<P>You can write a setup on your Mock of the Url helper using a similar syntax:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">UrlHelperMock
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">.Setup(x =&gt; x.RouteUrl("<SPAN style="COLOR: #8b0000">Show</SPAN>",<SPAN style="COLOR: #0000ff">new</SPAN>{ projectName="<SPAN style="COLOR: #8b0000">matt</SPAN>"}.AsMatch()))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">.Return("<SPAN style="COLOR: #8b0000">someUrlToTest</SPAN>");</PRE></PRE>
<P>Here is the code that defines the AsMatch extension method:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> ObjectMoqExtensions
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN> AsMatch(<SPAN style="COLOR: #0000ff">this</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN> @<SPAN style="COLOR: #0000ff">object</SPAN>)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> Match&lt;<SPAN style="COLOR: #0000ff">object</SPAN>&gt;.Create(testObject =&gt; DoObjectsMatch(@<SPAN style="COLOR: #0000ff">object</SPAN>, testObject));
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">bool</SPAN> DoObjectsMatch(<SPAN style="COLOR: #0000ff">object</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN>1, <SPAN style="COLOR: #0000ff">object</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN>2)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var props1 = ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN>1);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var props2 = ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN>2);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var query = from prop1 <SPAN style="COLOR: #0000ff">in</SPAN> props1
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                        join prop2 <SPAN style="COLOR: #0000ff">in</SPAN> props2 on prop1.Key equals prop2.Key
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                        select prop1.Value.Equals(prop2.Value);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> query.Count(x =&gt; x) == Math.Max(props1.Count(), props2.Count());
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> Dictionary&lt;<SPAN style="COLOR: #0000ff">string</SPAN>, <SPAN style="COLOR: #0000ff">object</SPAN>&gt; ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN> @<SPAN style="COLOR: #0000ff">object</SPAN>)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var dictionary = <SPAN style="COLOR: #0000ff">new</SPAN> Dictionary&lt;<SPAN style="COLOR: #0000ff">string</SPAN>, <SPAN style="COLOR: #0000ff">object</SPAN>&gt;();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var properties = TypeDescriptor.GetProperties(@<SPAN style="COLOR: #0000ff">object</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">foreach</SPAN> (PropertyDescriptor property <SPAN style="COLOR: #0000ff">in</SPAN> properties)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                dictionary.Add(property.Name, property.GetValue(@<SPAN style="COLOR: #0000ff">object</SPAN>));
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> dictionary;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }</PRE></PRE><img src="http://blogs.msdn.com/aggbug.aspx?PostID=9928293" width="1" height="1">]]></content:encoded>
			<wfw:commentRss>http://matthewmanela.com/2009/11/24/useful-moq-extension-method-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful Moq Extension Method</title>
		<link>http://matthewmanela.com/2009/11/24/useful-moq-extension-method-3/</link>
		<comments>http://matthewmanela.com/2009/11/24/useful-moq-extension-method-3/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 00:03:00 +0000</pubDate>
		<dc:creator>MattManela</dc:creator>
				<category><![CDATA[ASP .NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Moq]]></category>

		<guid isPermaLink="false">/b/matt/archive/2009/11/24/useful-moq-extension-method.aspx</guid>
		<description><![CDATA[I have been working with ASP .NET MVC and I use the Moq mocking library to help test the code I write.&#160;&#160; Often in ASP MVC anonymous objects are passed around as function arguments.&#160; This is especially common in calls to RouteUrl.&#160; S...]]></description>
			<content:encoded><![CDATA[<P>I have been working with ASP .NET MVC and I use the <A href="http://code.google.com/p/moq/" mce_href="http://code.google.com/p/moq/">Moq</A> mocking library to help test the code I write.&nbsp;&nbsp; Often in ASP MVC anonymous objects are passed around as function arguments.&nbsp; This is especially common in calls to RouteUrl.&nbsp; Since I want to be able to test this and verify that it is called correctly I wrote a handy extension method to make it easier called <STRONG>AsMatch</STRONG>.</P>
<P>To be able to test the UrlHelper (since it doesn't have an interface or virtual methods) I wrote a simple wrapper around it with an interface to enable testing.&nbsp; Using that with this extension method makes testing route generation a breeze.</P>
<P>Given a call to generate a Url like this:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">var url = Url.RouteUrl("<SPAN style="COLOR: #8b0000">Show</SPAN>", <SPAN style="COLOR: #0000ff">new</SPAN> { projectName="<SPAN style="COLOR: #8b0000">matt</SPAN>" });</PRE></PRE>
<P>You can write a setup on your Mock of the Url helper using a similar syntax:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">UrlHelperMock
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">.Setup(x =&gt; x.RouteUrl("<SPAN style="COLOR: #8b0000">Show</SPAN>",<SPAN style="COLOR: #0000ff">new</SPAN>{ projectName="<SPAN style="COLOR: #8b0000">matt</SPAN>"}.AsMatch()))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">.Return("<SPAN style="COLOR: #8b0000">someUrlToTest</SPAN>");</PRE></PRE>
<P>Here is the code that defines the AsMatch extension method:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> ObjectMoqExtensions
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN> AsMatch(<SPAN style="COLOR: #0000ff">this</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN> @<SPAN style="COLOR: #0000ff">object</SPAN>)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> Match&lt;<SPAN style="COLOR: #0000ff">object</SPAN>&gt;.Create(testObject =&gt; DoObjectsMatch(@<SPAN style="COLOR: #0000ff">object</SPAN>, testObject));
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">bool</SPAN> DoObjectsMatch(<SPAN style="COLOR: #0000ff">object</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN>1, <SPAN style="COLOR: #0000ff">object</SPAN> <SPAN style="COLOR: #0000ff">object</SPAN>2)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var props1 = ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN>1);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var props2 = ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN>2);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var query = from prop1 <SPAN style="COLOR: #0000ff">in</SPAN> props1
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                        join prop2 <SPAN style="COLOR: #0000ff">in</SPAN> props2 on prop1.Key equals prop2.Key
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                        select prop1.Value.Equals(prop2.Value);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> query.Count(x =&gt; x) == Math.Max(props1.Count(), props2.Count());
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> Dictionary&lt;<SPAN style="COLOR: #0000ff">string</SPAN>, <SPAN style="COLOR: #0000ff">object</SPAN>&gt; ToDictionary(<SPAN style="COLOR: #0000ff">object</SPAN> @<SPAN style="COLOR: #0000ff">object</SPAN>)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var dictionary = <SPAN style="COLOR: #0000ff">new</SPAN> Dictionary&lt;<SPAN style="COLOR: #0000ff">string</SPAN>, <SPAN style="COLOR: #0000ff">object</SPAN>&gt;();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var properties = TypeDescriptor.GetProperties(@<SPAN style="COLOR: #0000ff">object</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">foreach</SPAN> (PropertyDescriptor property <SPAN style="COLOR: #0000ff">in</SPAN> properties)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                dictionary.Add(property.Name, property.GetValue(@<SPAN style="COLOR: #0000ff">object</SPAN>));
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> dictionary;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }</PRE></PRE><img src="http://blogs.msdn.com/aggbug.aspx?PostID=9928293" width="1" height="1">]]></content:encoded>
			<wfw:commentRss>http://matthewmanela.com/2009/11/24/useful-moq-extension-method-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
