<?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>Circles and Crosses &#187; MVC</title>
	<atom:link href="http://ox.no/posts/tag/mvc/feed" rel="self" type="application/rss+xml" />
	<link>http://ox.no</link>
	<description>Håvard Stranden&#039;s website</description>
	<lastBuildDate>Sat, 20 Aug 2011 00:11:11 +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>Mocking HtmlHelper in ASP.NET MVC 2 and 3 using Moq</title>
		<link>http://ox.no/posts/mocking-htmlhelper-in-asp-net-mvc-2-and-3-using-moq</link>
		<comments>http://ox.no/posts/mocking-htmlhelper-in-asp-net-mvc-2-and-3-using-moq#comments</comments>
		<pubDate>Wed, 11 Aug 2010 08:51:39 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://ox.no/?p=226</guid>
		<description><![CDATA[Still having trouble mocking HtmlHelper? This is an update to my previous post on mocking HtmlHelper way back when ASP.NET MVC RC1 was released. Eric notified me through a comment on the post and a question on StackOverflow that the &#8230; <a href="http://ox.no/posts/mocking-htmlhelper-in-asp-net-mvc-2-and-3-using-moq">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Still having trouble mocking HtmlHelper? This is an update to <a href="http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq">my previous post on mocking HtmlHelper</a> way back when ASP.NET MVC RC1 was released. Eric notified me through <a href="http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq#comment-840">a comment</a> on the post and a <a href="http://stackoverflow.com/questions/3445770/missingmethodexception-thrown-when-trying-to-mock-htmlhelper-with-moq">question on StackOverflow</a> that the code for ASP.NET MVC RC1 did not work with ASP.NET MVC 2.  The code in this post should work with ASP.NET MVC 2 and ASP.NET MVC 3 Preview 1.</p>

<p><pre class="brush: csharp; ">

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
    Mock&lt;ViewContext&gt; mockViewContext = new Mock&lt;ViewContext&gt;(
        new ControllerContext(
            new Mock&lt;HttpContextBase&gt;().Object,
            new RouteData(),
            new Mock&lt;ControllerBase&gt;().Object),
        new Mock&lt;IView&gt;().Object,
        vd,
        new TempDataDictionary(),
        new Mock&lt;TextWriter&gt;().Object);
    var mockViewDataContainer = new Mock&lt;IViewDataContainer&gt;();
    mockViewDataContainer.Setup(v =&gt; v.ViewData)
        .Returns(vd);
    return new HtmlHelper(mockViewContext.Object,
                            mockViewDataContainer.Object);
}

</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/mocking-htmlhelper-in-asp-net-mvc-2-and-3-using-moq/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Mocking HtmlHelper in ASP.NET MVC RC1 using Moq</title>
		<link>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq</link>
		<comments>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq#comments</comments>
		<pubDate>Sun, 08 Mar 2009 17:15:13 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://ox.no/?p=65</guid>
		<description><![CDATA[For those of you trying to mock HtmlHelper, but finding it difficult, here&#8217;s a mock that works in ASP.NET MVC RC1. The ViewDataDictionary that is passed to the HtmlHelper can be empty, or made to contain the data you want &#8230; <a href="http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For those of you trying to mock HtmlHelper, but finding it difficult, here&#8217;s a mock that works in ASP.NET MVC RC1.</p>

<p><span id="more-65"></span></p>

<p>The <code>ViewDataDictionary</code> that is passed to the <code>HtmlHelper</code> can be empty, or made to contain the data you want for your test.</p>

<p><pre class="brush: csharp; ">

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
  var mockViewContext = new Mock&lt;ViewContext&gt;(
    new ControllerContext(
      new Mock&lt;HttpContextBase&gt;().Object,
      new RouteData(),
      new Mock&lt;ControllerBase&gt;().Object),
    new Mock&lt;IView&gt;().Object,
    vd,
    new TempDataDictionary());&lt;/p&gt;

&lt;p&gt;var mockViewDataContainer = new Mock&lt;IViewDataContainer&gt;();
  mockViewDataContainer.Setup(v =&gt; v.ViewData)
    .Returns(vd);&lt;/p&gt;

&lt;p&gt;return new HtmlHelper(mockViewContext.Object, 
    mockViewDataContainer.Object);
}

</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/mocking-htmlhelper-in-aspnet-mvc-rc1-using-moq/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

