<?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>My Public Interface &#187; RhinoMocks</title>
	<atom:link href="http://blog.roboblob.com/tag/rhinomocks/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.roboblob.com</link>
	<description>and ideas for its refactoring...</description>
	<lastBuildDate>Fri, 16 Jul 2010 14:55:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Unit testing and mocking internal interfaces with RhinoMocks</title>
		<link>http://blog.roboblob.com/2010/02/18/unit-testing-and-mocking-internal-interfaces-with-rhinomocks/</link>
		<comments>http://blog.roboblob.com/2010/02/18/unit-testing-and-mocking-internal-interfaces-with-rhinomocks/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 21:48:07 +0000</pubDate>
		<dc:creator>roboblob</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Mocks]]></category>
		<category><![CDATA[RhinoMocks]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.roboblob.com/?p=234</guid>
		<description><![CDATA[Im not the kind of purist that will avoid unit testing internal classes. If its some very important code i want to test it even if its internal.

Another case when i want to access internal classes is when i want to test some public class that uses some internal classes (via their interfaces).
In this scenario i need to be able to mock those internal interfaces that the public class is using in order to be able to instantiate it and unit test it.]]></description>
			<content:encoded><![CDATA[Number of View: 2289<br/><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Im working on a project that i want to cover with unit tests as much as possible.</p>
<p>Problem is that most of the things i need to test are kept internal inside some libraries.</p>
<p>Im not the kind of purist that will avoid unit testing internal classes. If its some very important code i want to test it even if its internal.</p>
<p>Another case when i want to access internal classes is when i want to test some public class that uses some internal classes (via their interfaces).<br />
In this scenario i need to be able to mock those internal interfaces that the public class is using in order to be able to instantiate it and unit test it.</p>
<p>Now some of you experienced TDD people will shout: &#8220;Ha haaa that&#8217;s easy just use the</p>
<p>[assembly: InternalsVisibleTo("Some.TestAssembly.Name")] and your are cool&#8221;.</p>
<p>But this is only partial true. Here is why:<br />
Using the InternalsVisibleTo attribute will allow you to access internal members of assembly from the test assembly and use them in unit tests, but if you try to mock them (lets say we use the fantastic <a href="http://www.ayende.com/projects/rhino-mocks.aspx" target="_blank">RhinoMocks</a>), you will get exception similar to this one:</p>
<p><span style="color: #ff0000;">Castle.DynamicProxy.Generators.GeneratorException: Type is not public, so a proxy cannot be generated. </span></p>
<p><span style="color: #ff0000;">Type: Roboblob.Implementors.Interfaces.ICookieService</span></p>
<p>So in this concrete case I have added the <strong>InternalsVisibleTo </strong>attribute to my main assembly and this allowed the other assembly where my unit tests are to access the internal interface ICookieService but this helped me only half-way. I forgot about RhinoMocks!.</p>
<p>So RhinoMocks cannot create proxy that implements this interface in order to create a mock and throws this exception.</p>
<p>I tried few things to solve this and then i found this link: <a href="http://ayende.com/Wiki/Rhino+Mocks+-+Internal+Methods.ashx" target="_blank">Rhino Mocks Internal Members</a>.</p>
<p>As most things in life, its very simple once you figure it out: In order to be able to mock internal members of some class you need to allow the RhinoMocks to &#8216;see&#8217; internals of this class by placing this in your AssemblyInfo.cs:</p>
<pre class="brush: csharp;">
[assembly: InternalsVisibleTo (&quot;DynamicProxyGenAssembly2&quot;)]
</pre>
<p>Since RhinoMocks must create a proxy class to create a mock and definition for that proxy is in DynamicProxyGenAssembly2 &#8211; we need to allow this assembly to access internal members of the assembly holding the class we are testing.</p>
<p>So with this new trick I&#8217;m ready to dive into my mocking adventures&#8230;.</p>
<p>Wish me luck <img src='http://blog.roboblob.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F&amp;title=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F&amp;title=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F&amp;title=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F&amp;headline=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks&amp;url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks&amp;u=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks&amp;url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks&amp;url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks&amp;url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F&amp;title=Unit+testing+and+mocking+internal+interfaces+with+RhinoMocks&amp;summary=&amp;source=" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fblog.roboblob.com%2F2010%2F02%2F18%2Funit-testing-and-mocking-internal-interfaces-with-rhinomocks%2F" ><img class="lightsocial_img" src="http://blog.roboblob.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://blog.roboblob.com/2010/02/18/unit-testing-and-mocking-internal-interfaces-with-rhinomocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
