<?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>DotNetSurfers</title>
	<atom:link href="http://dotnetsurfers.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://dotnetsurfers.com/blog</link>
	<description>Latish Sehgal&#039;s Blog</description>
	<lastBuildDate>Mon, 30 Aug 2010 21:24:57 +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>BarCamp Dallas Review</title>
		<link>http://dotnetsurfers.com/blog/2010/08/21/barcamp-dallas-review/</link>
		<comments>http://dotnetsurfers.com/blog/2010/08/21/barcamp-dallas-review/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 03:55:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[community]]></category>
		<category><![CDATA[presentation]]></category>

		<guid isPermaLink="false">http://dotnetsurfers.com/blog/2010/08/21/barcamp-dallas-review/</guid>
		<description><![CDATA[<p>I went to my first <a href="http://barcamp.org/BarCampDallas" target="_blank">Barcamp</a> today, held at <a href="http://improvingenterprises.com/" target="_blank">Improving Enterprises</a> in Dallas. It was a technology agnostic event, and It was good to see the cool things developers on other platforms are playing with. I&#8230; <a href="http://dotnetsurfers.com/blog/2010/08/21/barcamp-dallas-review/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p>I went to my first <a href="http://barcamp.org/BarCampDallas" target="_blank">Barcamp</a> today, held at <a href="http://improvingenterprises.com/" target="_blank">Improving Enterprises</a> in Dallas. It was a technology agnostic event, and It was good to see the cool things developers on other platforms are playing with. I also gave a condensed version of <a href="http://dotnetsurfers.com/blog/2010/05/10/bdd-mspec-presentation-at-the-dallas-c-sig/" target="_blank">my BDD talk</a> and it was pretty well received. The following topics were presented: </p>
<ul>
<li>Visual Studio Tips &amp; Tricks by <a href="http://blogs.msdn.com/b/zainnab/" target="_blank">Zain Naboulsi</a><a href="http://blogs.msdn.com/b/zainnab/):">:</a> Zain covered some useful Visual Studio tips and tricks that he has been outlining on twitter and his blog lately. Of course, the most remarkable shortcut he showed (and nobody in the audience knew about) was that pressing F7 in a command prompt gives a dropdown of all previously executed commands <img src='http://dotnetsurfers.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</li>
<li>Delightful forms plug-in for Ruby On Rails: The speaker outlined his inspiration behind this forms plug-in, and the output looked very clean and user friendly.</li>
<li>The CAP theorem by <a href="http://qedcode.com/blogs/michael-l-perry" target="_blank">Michael Perry</a><a href="http://qedcode.com/blogs/michael-l-perry):">:</a> Michael gave an overview of the CAP theorem (only 2 out of 3 features from the list of Consistency, Availability and Partition Tolerance can be achieved at one time for a distributed computer system).</li>
<li>NoSQL/MongoDb 101.</li>
<li><a href="http://www.foldertrack.com/" target="_blank">FolderTrack</a> application overview.</li>
<li><a href="http://lesscss.org/" target="_blank">Less CSS</a><a href="http://lesscss.org/):">:</a> allows use of variables, mixins and nested rules in CSS.</li>
<li>Windows Phone 7 overview by <a href="http://chriskoenig.net/" target="_blank">Chris Koenig</a>.</li>
<li>SEO optimization and hacks: This was my favorite session for the day and the presenter showed very impressive hacks and examples for manipulating SEO.</li>
<li>Targeting Mobile CSS media.</li>
<li>Overview of <a href="http://www.dallasmakerspace.com/blog/about-us/" target="_blank">DallasMakerSpace community</a><a href="http://www.dallasmakerspace.com/blog/about-us/):">:</a> Another cool presentation about the hackerspace community in Dallas. </li>
<li>My <a href="http://dotnetsurfers.com/blog/2010/05/10/bdd-mspec-presentation-at-the-dallas-c-sig/" target="_blank">BDD talk</a>.</li>
</ul>
<p>&#160;</p>
<p>A big thanks to the organizers for putting this up and I look forward to attending this next year.</p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/08/21/barcamp-dallas-review/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Useful MacBook accessories</title>
		<link>http://dotnetsurfers.com/blog/2010/08/19/useful-macbook-accessories/</link>
		<comments>http://dotnetsurfers.com/blog/2010/08/19/useful-macbook-accessories/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 01:14:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://dotnetsurfers.com/blog/2010/08/19/useful-macbook-accessories/</guid>
		<description><![CDATA[<p>I have <a href="http://www.dotnetsurfers.com/Blog/2009/07/31/MyNewMacBookPro.aspx" target="_blank">had my MacBook Pro</a> for a year now and it’s my primary machine both at work and at home. I have since upgraded it with <a href="http://www.newegg.com/Product/Product.aspx?Item=N82E16820231297" target="_blank">8GB RAM</a> (which has come down in price by&#8230; <a href="http://dotnetsurfers.com/blog/2010/08/19/useful-macbook-accessories/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p>I have <a href="http://www.dotnetsurfers.com/Blog/2009/07/31/MyNewMacBookPro.aspx" target="_blank">had my MacBook Pro</a> for a year now and it’s my primary machine both at work and at home. I have since upgraded it with <a href="http://www.newegg.com/Product/Product.aspx?Item=N82E16820231297" target="_blank">8GB RAM</a> (which has come down in price by more than half in the last 9 months) and a 160GB SSD. At my day job, I work with <a href="http://dotnetsurfers.com/blog/2009/08/15/working-with-visual-studio-on-a-mac/" target="_blank">Visual Studio inside a VMWare Fusion VM</a> and have been pretty happy with the performance. Here are some accessories that I have found useful for my MBP:</p>
<ol>
<li><a href="http://www.amazon.com/gp/product/B0029TNH3C" target="_blank">Screen Protector</a><a href="http://www.amazon.com/gp/product/B0029TNH3C):">:</a> I bought the glossy screen MBP, and that turned out to be a big mistake since I&#160; frequently hack at coffee shop/restaurant patios and the glossy screen made the code unreadable. This Moshi iVisor screen protector fixed that and gave the screen a matte finish. It was also very easy to apply without any air bubble issues.</li>
<li><a href="http://www.amazon.com/gp/product/B001QKI2DA" target="_blank">Keyboard Cover</a><a href="http://www.amazon.com/gp/product/B001QKI2DA):">:</a> I bought the keyboard cover after it showed up in my Amazon recommended products list (probably because the screen protector is from the same company), and I have been pleasantly surprised with it. It is quite thin and does not take much time to get used to. It has also already saved me from serious trouble once when I spilled Diet Coke on the keyboard.</li>
<li>Spare <a href="http://store.apple.com/us/product/MC556LL/A?n=notebook_power_adapters&amp;fnode=MTY1NDEwMQ&amp;mco=MTc0NzA0MjY&amp;s=topSellers" target="_blank">Apple MagSafe Power Adapter</a><a href="http://store.apple.com/us/product/MC556LL/A?n=notebook_power_adapters&amp;fnode=MTY1NDEwMQ&amp;mco=MTc0NzA0MjY&amp;s=topSellers):">:</a> I haven&#8217;t been able to find a good docking solution for the MBP, but carrying a spare power adapter saves me the trouble of crawling under the desk every day to plug/unplug it. </li>
<li><a href="http://store.apple.com/us/product/MB441Z/A" target="_blank">Apple MagSafe Airline Adapter</a>: I have already flown to India (&gt;20 hour flying time) twice this year, and the airline adapter is a lifesaver for such long flights. Just make sure you get a seat with a power port (I use <a href="http://www.seatguru.com" target="_blank">www.seatguru.com</a> to choose good seats), and you are good to go.</li>
<li><a href="http://www.amazon.com/mStand-Laptop-Stand-Rain-Design/dp/B000OOYECC/ref=sr_1_2?ie=UTF8&amp;s=electronics&amp;qid=1282266438&amp;sr=8-2" target="_blank">mStand Laptop Stand</a>: I use this at home, and find it to be quite elegant looking. It also has a gap at the front for easy access to the laptop lid for opening/closing.</li>
</ol>
<p>I have been meaning to check out the <a href="http://www.hypershop.com/HyperMac-External-Battery-for-MacBook-iPhone-iPad-iPod-s/91.htm" target="_blank">HyperMac external battery</a>, looks like a great product if you go to conferences frequently, or are a road warrior. If any of you have tried it out, please do leave a comment about your experience with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/08/19/useful-macbook-accessories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with a Windows Workflow Service in a non-Workflow client project</title>
		<link>http://dotnetsurfers.com/blog/2010/08/17/working-with-a-windows-workflow-service-in-a-non-workflow-client-project/</link>
		<comments>http://dotnetsurfers.com/blog/2010/08/17/working-with-a-windows-workflow-service-in-a-non-workflow-client-project/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 02:19:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Windows Workflow]]></category>

		<guid isPermaLink="false">http://dotnetsurfers.com/blog/2010/08/17/working-with-a-windows-workflow-service-in-a-non-workflow-client-project/</guid>
		<description><![CDATA[<p>I just wasted a day trying to figure out something that should have worked out of the box with Visual Studio. Hopefully, this can save somebody some time. I have been working with Windows Workflow (.Net 4, Visual Studio 2010)&#8230; <a href="http://dotnetsurfers.com/blog/2010/08/17/working-with-a-windows-workflow-service-in-a-non-workflow-client-project/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p>I just wasted a day trying to figure out something that should have worked out of the box with Visual Studio. Hopefully, this can save somebody some time. I have been working with Windows Workflow (.Net 4, Visual Studio 2010) over the last week and my project required me to call a Workflow Service from another workflow hosted within a Windows/NT Service. I created the Workflow Service and when I built a test client for it using a Workflow Console application, the activities from the service show up just fine in the client workflow. </p>
<p>However, when I add reference for the Workflow service in the NT Service, no new activities are available in a workflow inside the NT Service project. It looks like Visual Studio parses the service reference differently based on the type of client project. I ended up comparing the csproj files manually for my console workflow client and NT service workflow client. By copying the following entry to the NT service project file, I was able to get it to behave consistently:    <br /><font color="#0000ff">&lt;ProjectTypeGuids&gt;{32f31d43-81cc-4c15-9de6-3fc5453562b6};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}&lt;/ProjectTypeGuids&gt;</font> </p>
<p>It seems that the first guid represents a workflow project type and the second one stands for a Windows C# project.</p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/08/17/working-with-a-windows-workflow-service-in-a-non-workflow-client-project/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Migrating from dasBlog to WordPress</title>
		<link>http://dotnetsurfers.com/blog/2010/07/21/migrating-from-dasblog-to-wordpress/</link>
		<comments>http://dotnetsurfers.com/blog/2010/07/21/migrating-from-dasblog-to-wordpress/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 23:43:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dotnetsurfers.com/blog/2010/07/21/migrating-from-dasblog-to-wordpress/</guid>
		<description><![CDATA[<p>I migrated this blog from dasBlog to WordPress a couple of weeks ago, to take advantage of the large number of WordPress plug-ins and themes available. Here are the steps I had to follow:    <br />1. Created Local backup of&#8230; <a href="http://dotnetsurfers.com/blog/2010/07/21/migrating-from-dasblog-to-wordpress/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p>I migrated this blog from dasBlog to WordPress a couple of weeks ago, to take advantage of the large number of WordPress plug-ins and themes available. Here are the steps I had to follow:    <br />1. Created Local backup of my dasBlog site.     <br />2. Used <a href="http://merill.net/2008/03/dasblog-to-blogml-converter/" target="_blank">this tool</a> to create BlogML file from dasBlog.     <br />3. Installed WordPress on a test directory on my host (GoDaddy), since it provided a 1-click install.     <br />4. Configured the permalink structure in WordPress.     <br />5. Used <a href="http://www.kavinda.net/content/other/BlogML-WordPress-Import.zip" target="_blank">BlogML to WordPress Import Module</a> mentioned <a href="http://www.kavinda.net/2008/10/23/migrating-from-dasblog-to-wordpress.html" target="_blank">here</a> to import all my old data.     <br />6. Installed Redirection plug-in for WordPress. Under Redirection-&gt;Options-&gt;Import, I used the CSV file containing the mapped URLS to set up URL redirection so that all the old links remained intact.     <br />7. Renamed the test directory to my desired blog directory name in the host. </p>
<p>In case you are subscribed to the old RSS feed URL, please be sure to update it to the <a href="http://dotnetsurfers.com/blog/feed/" target="_blank">new one</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/07/21/migrating-from-dasblog-to-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Connecting to a local resource or service from the Smart Device Emulator in Visual Studio</title>
		<link>http://dotnetsurfers.com/blog/2010/06/06/connecting-to-a-local-resourceservice-from-the-smart-device-emulator-in-visual-studio/</link>
		<comments>http://dotnetsurfers.com/blog/2010/06/06/connecting-to-a-local-resourceservice-from-the-smart-device-emulator-in-visual-studio/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 18:27:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dotnetsurfers.com/blog/2010/06/06/connecting-to-a-local-resourceservice-from-the-smart-device-emulator-in-visual-studio/</guid>
		<description><![CDATA[<p>I am working on a project right now that has a smart device component, and we use the Pocket PC 2003 SE Emulator to test and debug the application locally. It took me a while to get the environment setup&#8230; <a href="http://dotnetsurfers.com/blog/2010/06/06/connecting-to-a-local-resourceservice-from-the-smart-device-emulator-in-visual-studio/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p>I am working on a project right now that has a smart device component, and we use the Pocket PC 2003 SE Emulator to test and debug the application locally. It took me a while to get the environment setup for the first time; hopefully this post can save some time for other people. <a href="http://blogs.msdn.com/b/akhune/archive/2005/11/16/493329.aspx" target="_blank">This post</a> helped me in getting the emulator connected to the internet. Accessing a web service on my local computer from inside the emulator was a little harder to figure out. It turns out that: </p>
<ol>
<li>You cannot use ‘localhost’ from inside the emulator to refer to your local PC. You have to use either the computer name or the IP address. However, even after I use the computer name, I get this when trying to access a local resource.      <br /><a href="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/emulator1.jpg"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="emulator1" border="0" alt="emulator1" src="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/emulator1_thumb.jpg" width="289" height="484" /></a> </li>
<li>The Windows firewall blocks the emulator from accessing anything on port 80 by default. I had to pull up the Windows Firewall settings and create a new Inbound Rule (I am using 64-bit Windows 7 here). The screenshots below should be self-explanatory.      <br /><a href="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall6.jpg"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="firewall6" border="0" alt="firewall6" src="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall6_thumb.jpg" width="599" height="449" /></a> <a href="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall1.jpg"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="firewall1" border="0" alt="firewall1" src="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall1_thumb.jpg" width="599" height="484" /></a><a href="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall2.jpg"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="firewall2" border="0" alt="firewall2" src="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall2_thumb.jpg" width="599" height="484" /></a>       <br /><a href="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall3.jpg"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="firewall3" border="0" alt="firewall3" src="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall3_thumb.jpg" width="599" height="484" /></a> <a href="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall4.jpg"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="firewall4" border="0" alt="firewall4" src="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall4_thumb.jpg" width="599" height="484" /></a> <a href="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall5.jpg"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="firewall5" border="0" alt="firewall5" src="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/firewall5_thumb.jpg" width="599" height="484" /></a> </li>
</ol>
</p>
</p>
<p>And Voila! The emulator is now able to access local resources.</p>
<p><a href="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/emulator2.jpg"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="emulator2" border="0" alt="emulator2" src="http://dotnetsurfers.com/blog/wp-content/uploads/2010/06/emulator2_thumb.jpg" width="289" height="484" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/06/06/connecting-to-a-local-resourceservice-from-the-smart-device-emulator-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>BDD &amp; MSpec presentation at the Dallas CSharp SIG</title>
		<link>http://dotnetsurfers.com/blog/2010/05/10/bdd-mspec-presentation-at-the-dallas-c-sig/</link>
		<comments>http://dotnetsurfers.com/blog/2010/05/10/bdd-mspec-presentation-at-the-dallas-c-sig/#comments</comments>
		<pubDate>Mon, 10 May 2010 12:43:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[presentation]]></category>

		<guid isPermaLink="false">http://dotnetsurfers.com/blog/2010/05/10/bdd-mspec-presentation-at-the-dallas-c-sig/</guid>
		<description><![CDATA[<p>Thanks to all who came to my BDD/MSpec talk. If you want, you can watch a video of the talk below.</p>
<p> <embed src="http://blip.tv/play/hYpfgdyrCgA" type="application/x-shockwave-flash" width="400" height="300" allowscriptaccess="always" allowfullscreen="true"></embed>
<p>&#160;</p>
<p>The code that we developed during the talk can be downloaded <a href="http://www.dotnetsurfers.com/utils/bowling.zip" target="_blank">here</a>.</p>
]]></description>
			<content:encoded><![CDATA[<p>Thanks to all who came to my BDD/MSpec talk. If you want, you can watch a video of the talk below.</p>
<p> <embed src="http://blip.tv/play/hYpfgdyrCgA" type="application/x-shockwave-flash" width="400" height="300" allowscriptaccess="always" allowfullscreen="true"></embed>
<p>&#160;</p>
<p>The code that we developed during the talk can be downloaded <a href="http://www.dotnetsurfers.com/utils/bowling.zip" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/05/10/bdd-mspec-presentation-at-the-dallas-c-sig/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started With Mocking &#8211; Part 2- Using MOQ</title>
		<link>http://dotnetsurfers.com/blog/2010/04/02/getting-started-with-mocking-part-2-using-moq/</link>
		<comments>http://dotnetsurfers.com/blog/2010/04/02/getting-started-with-mocking-part-2-using-moq/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 12:25:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MOQ]]></category>
		<category><![CDATA[mocking]]></category>

		<guid isPermaLink="false">http://www.DotNetSurfers.com/Blog/2010/04/02/GettingStartedWithMockingPart2UsingMOQ.aspx</guid>
		<description><![CDATA[<p><a href="http://www.dotnetsurfers.com/utils/mocking2.zip" target="_blank">Download Code</a></p>
<p>This is the second part of the 2 post series on Mocking. In the <a href="http://www.dotnetsurfers.com/Blog/2010/02/10/GettingStartedWithMockingPart1TheBasics.aspx" target="_blank">last post</a>, we discussed the basics of Mocking and how Adam (the intern) used Manual Mocking to unit test the simple Shopping&#8230; <a href="http://dotnetsurfers.com/blog/2010/04/02/getting-started-with-mocking-part-2-using-moq/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.dotnetsurfers.com/utils/mocking2.zip" target="_blank">Download Code</a></p>
<p>This is the second part of the 2 post series on Mocking. In the <a href="http://www.dotnetsurfers.com/Blog/2010/02/10/GettingStartedWithMockingPart1TheBasics.aspx" target="_blank">last post</a>, we discussed the basics of Mocking and how Adam (the intern) used Manual Mocking to unit test the simple Shopping Cart application he’s building. In this post, we’ll see how using a Mocking library can make the process of mocking out dependencies simpler.</p>
<p><strong>Why use a mocking library?      <br /></strong>The projects you work on are usually nowhere as simple/small as the Shopping Cart example Adam is working on (If they are, is there room for one more on the team?). If you have to build a new mock class for each of your classes, the amount of new code you have to write increases significantly. Also, you might want to reuse your test infrastructure across different classes, or just want the mock object to act dumb. </p>
<p>It’s also harder to do interaction based testing using Manual Mocks. In the manual mock for Logger, we had to add a new field to track how many times it is called. As the behavior to test becomes complicated, so does your mock class. Using a mocking library can take care of all these mundane things.</p>
<p>&#160;<strong>Why MOQ?</strong>     <br /><a href="http://code.google.com/p/moq/" target="_blank">MOQ</a> is a simple and easy to learn mocking library for .NET that takes advantage of .NET 3.5 (i.e. Linq expression trees) and C# 3.0 features (i.e. lambda expressions). There are other mocking libraries like RhinoMocks and TypeMock Isolator, and MOQ is relatively new to the scene. The main reason I recommend using MOQ is because it has a really low learning curve and is intuitive to use. Unlike other libraries, it does not force you to distinguish between a mock and a stub, which makes it easier for developers new to mocking to work with. </p>
<p><strong>MOQ Basics</strong>     <br />To create a mock of type T, you will use the Mock&lt;T&gt; class. Any interface type can be used for mocking, but for classes, only abstract and virtual members can be mocked. Let’s walk through a few common scenarios and the corresponding MOQ usage.</p>
<p align="left">Creating Mock objects</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum"> </span>Mock&lt;IProduct&gt; _mockProduct = <span class="kwrd">new</span> Mock&lt;IProduct&gt;();</pre>
</div>
<div class="csharpcode">
<pre><span class="lnum"> </span>Mock&lt;ILogger&gt; _mockLogger = <span class="kwrd">new</span> Mock&lt;ILogger&gt;();</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Mocking a method implementation and returning fake data</p>
<div class="csharpcode">
<pre class="alt"> _mockProduct.Setup(m =&gt; m.GetProductCategory()).Returns(<span class="str">&quot;Test Category&quot;</span>);</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<p>Mocking a property and returning fake data</p>
<div class="csharpcode">
<pre class="alt">_mockProduct.SetupGet(m =&gt; m.Name)</pre>
<pre>            .Returns(<span class="str">&quot;Product 1&quot;</span>);</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Throw exception</p>
<div class="csharpcode">
<pre class="alt"> _mockLogger.Setup(m =&gt; m.Log(It.Is&lt;<span class="kwrd">string</span>&gt;(p =&gt; p == <span class="kwrd">null</span>)))</pre>
<pre>            .Throws(<span class="kwrd">new</span> ArgumentNullException());</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<p>Validate Parameters</p>
<div class="csharpcode">
<pre class="alt"> _mockLogger.Setup(m =&gt; m.Log(It.IsRegex(<span class="str">&quot;[1-9]+&quot;</span>)))</pre>
<pre>            .Callback(() =&gt; Console.WriteLine(<span class="str">&quot;Numbers passed&quot;</span>));</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Verify Interactions</p>
<div class="csharpcode">
<pre class="alt"> _mockLogger.Object.Log(<span class="str">&quot;test&quot;</span>);</pre>
<pre> _mockLogger.Verify(m =&gt; m.Log(It.Is&lt;<span class="kwrd">string</span>&gt;(s=&gt;s==<span class="str">&quot;test&quot;</span>)),Times.Once());</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Invoke Callback</p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd"> int</span> counter = 0;</pre>
<pre> _mockLogger.Setup(m =&gt; m.Log(It.IsAny&lt;String&gt;())).Callback(() =&gt; counter++);</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Even if you are new to MOQ, you’ll find the API very intuitive and easy to pick up.</p>
<p><strong>Back to the Shopping Cart</strong></p>
<p>So Adam sees how easy it is easy to work with MOQ and how much code/effort it saves him, and he updates his unit tests for the Shopping Cart accordingly. Note that these unit tests are independent of the manual mock classes as well as from external resources like the filesystem/database.</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>[TestMethod]</pre>
<pre><span class="lnum">   2:  </span><span class="kwrd">public</span> <span class="kwrd">void</span> AddProduct_AddingProductWithPrice10_ShouldMakeTotal10()</pre>
<pre class="alt"><span class="lnum">   3:  </span>{</pre>
<pre><span class="lnum">   4:  </span>    var mock = <span class="kwrd">new</span> Mock&lt;IProduct&gt;();</pre>
<pre class="alt"><span class="lnum">   5:  </span>    mock.SetupGet(m =&gt; m.Price).Returns(10.00M);</pre>
<pre><span class="lnum">   6:  </span>    var cart = <span class="kwrd">new</span> ShoppingCart(<span class="kwrd">null</span>);</pre>
<pre class="alt"><span class="lnum">   7:  </span>    cart.AddProduct(mock.Object);</pre>
<pre><span class="lnum">   8:  </span>    Assert.AreEqual(10.00M, cart.Total);</pre>
<pre class="alt"><span class="lnum">   9:  </span>&#160;</pre>
<pre><span class="lnum">  10:  </span>}</pre>
<pre class="alt"><span class="lnum">  11:  </span>&#160;</pre>
<pre><span class="lnum">  12:  </span>[TestMethod]</pre>
<pre class="alt"><span class="lnum">  13:  </span><span class="kwrd">public</span> <span class="kwrd">void</span> AddProduct_AddingProduct_ShouldCallLogger()</pre>
<pre><span class="lnum">  14:  </span>{</pre>
<pre class="alt"><span class="lnum">  15:  </span>    var mockProduct = <span class="kwrd">new</span> Mock&lt;IProduct&gt;();</pre>
<pre><span class="lnum">  16:  </span>    var mockLogger = <span class="kwrd">new</span> Mock&lt;ILogger&gt;();</pre>
<pre class="alt"><span class="lnum">  17:  </span>    mockLogger.Setup(m =&gt; m.Log(It.IsAny&lt;<span class="kwrd">string</span>&gt;())).Verifiable(<span class="str">&quot;Log should have been called&quot;</span>);</pre>
<pre><span class="lnum">  18:  </span>    var cart = <span class="kwrd">new</span> ShoppingCart(mockLogger.Object);</pre>
<pre class="alt"><span class="lnum">  19:  </span>    cart.AddProduct(mockProduct.Object);</pre>
<pre><span class="lnum">  20:  </span>    mockLogger.Verify();</pre>
<pre class="alt"><span class="lnum">  21:  </span>}</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p><strong>Later….</strong></p>
<p>You are satisfied with the ShoppingCart project and the corresponding tests that Adam built. When you run into him in the evening, you commend him on the nice work and invite him to have a couple of beers with you. As you are sitting at the bar, you glance at the mirror and you see something that makes the hair at the back of your neck stand up. Instead of Adam and you at the bar, it’s just you sitting there with 2 beers in your hand talking to yourself. You then realize that Adam was a figment of your imagination and you might have suffered from some sort of a dual personality disorder (Kind of like <a href="http://en.wikipedia.org/wiki/Fight_Club_(film)" target="_blank">Fight Club</a>). Your brain created Adam so that you could face your fears and misconceptions about Unit Testing and Mocking. On the bright side, you realize that you have finished your project and are sitting in a bar with 2 beers. Cheers!</p>
<p><a href="http://www.dotnetsurfers.com/utils/mocking2.zip" target="_blank">Download Code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/04/02/getting-started-with-mocking-part-2-using-moq/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting Started with Mocking &#8211; Part 1 &#8211; The Basics</title>
		<link>http://dotnetsurfers.com/blog/2010/02/10/getting-started-with-mocking-part-1-the-basics/</link>
		<comments>http://dotnetsurfers.com/blog/2010/02/10/getting-started-with-mocking-part-1-the-basics/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 13:50:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MOQ]]></category>
		<category><![CDATA[mocking]]></category>

		<guid isPermaLink="false">http://www.DotNetSurfers.com/Blog/2010/02/10/GettingStartedWithMockingPart1TheBasics.aspx</guid>
		<description><![CDATA[<p><a href="http://www.dotnetsurfers.com/utils/mocking1.zip" target="_blank">Download Code</a>.</p>
<p>This 2 part blog post series is based on a <a href="http://www.dotnetsurfers.com/Blog/2010/01/07/DallasCSharpSIGPresentationCodeAndSlides.aspx" target="_blank">talk</a> I gave at the Dallas C# SIG last month. In this post, I describe general mocking concepts that are independent of mocking frameworks. In the&#8230; <a href="http://dotnetsurfers.com/blog/2010/02/10/getting-started-with-mocking-part-1-the-basics/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.dotnetsurfers.com/utils/mocking1.zip" target="_blank">Download Code</a>.</p>
<p>This 2 part blog post series is based on a <a href="http://www.dotnetsurfers.com/Blog/2010/01/07/DallasCSharpSIGPresentationCodeAndSlides.aspx" target="_blank">talk</a> I gave at the Dallas C# SIG last month. In this post, I describe general mocking concepts that are independent of mocking frameworks. In the next post, I’ll describe how using a mocking framework (like MOQ) can reduce development time and effort. </p>
<p>You are probably familiar with Unit Tests, but let’s spend a little time revisiting them. As per <a href="http://weblogs.asp.net/rosherove/archive/2009/09/28/unit-test-definition-2-0.aspx" target="_blank">Roy Osherove</a>  <br />              “A unit test is a fast, in-memory, consistent, automated and repeatable test of a functional unit-of-work in the system.”     <br />This is different from writing Integration Tests, which can talk to external resources such as the filesystem, webservices, database and are inherently slow. A common mistake is to create a Unit Test Suite but fill it with Integration tests. This increases the test execution time and discourages developers from running the Unit Tests on a regular basis (Ideally, each time before checking in code or more frequently).</p>
<p>For the purpose of this series, lets assume that you are a senior developer at a firm with plenty of work on your hands. Your pointy haired manager stops by to tell you that he has a new project for you. Seeing that you are busy, he offers to assign you an intern, Adam. You reluctantly agree and talk to Adam who seems like a sharp, reserved guy with a good grasp over OOPs concepts. You go over the project requirements with him, which consist of building a simple Shopping Cart. The Shopping Cart should </p>
<ol>
<li>have the capability to add a product and update price accordingly, </li>
<li>have some sort of logging functionality. </li>
</ol>
<p> Adam seems confident that he can build this project, When asked about Unit Testing, he admits not having any prior experience with it. You tell him to look it up, and write Unit Tests for his code as well. So Adam goes into his cave, and starts working on the Shopping Cart app.He informs you after 2 days that he has now completed the application and his code is ready for review. The basic design looks like this
</p>
<p><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="MOQ_ClassDiagram[1]" border="0" alt="MOQ_ClassDiagram[1]" src="http://dotnetsurfers.com/blog/images/moq_classdiagram.jpg" width="398" height="484" /></p>
</p>
<p>The code for the <strong>ShoppingCart</strong> class is pretty simple. It has a <strong>Total</strong> property denoting the total price of the Shopping Cart, and an <strong>AddProduct()</strong> function, which adds the price of the product to the total and logs a message. The logger object is passed to the <strong>ShoppingCart</strong> via constructor injection. Also, the ShoppingCart is coded against IProduct and ILogger interfaces rather than concrete implementations, which makes testing easier.</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span> <span class="kwrd">public</span> <span class="kwrd">class</span> ShoppingCart</pre>
<pre><span class="lnum">   2:  </span>    {</pre>
<pre class="alt"><span class="lnum">   3:  </span>        <span class="kwrd">public</span> ShoppingCart(ILogger logger)</pre>
<pre><span class="lnum">   4:  </span>        {</pre>
<pre class="alt"><span class="lnum">   5:  </span>            <span class="kwrd">this</span>._logger = logger;</pre>
<pre><span class="lnum">   6:  </span>        }</pre>
<pre class="alt"><span class="lnum">   7:  </span> </pre>
<pre><span class="lnum">   8:  </span>        <span class="kwrd">private</span> ILogger _logger;</pre>
<pre class="alt"><span class="lnum">   9:  </span>        <span class="kwrd">public</span> <span class="kwrd">decimal</span> Total { get; set; }</pre>
<pre><span class="lnum">  10:  </span> </pre>
<pre class="alt"><span class="lnum">  11:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> AddProduct(IProduct product)</pre>
<pre><span class="lnum">  12:  </span>        {</pre>
<pre class="alt"><span class="lnum">  13:  </span>            Total = Total + product.Price;</pre>
<pre><span class="lnum">  14:  </span>            <span class="kwrd">if</span> (_logger != <span class="kwrd">null</span>)</pre>
<pre class="alt"><span class="lnum">  15:  </span>                _logger.Log(String.Format(<span class="str">"Product {0} has been added."</span>,product.Name));</pre>
<pre><span class="lnum">  16:  </span>        }</pre>
<pre class="alt"><span class="lnum">  17:  </span>    }</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>We don’t really care about the Logger and Product implementation, but just know that the Logger logs to a text file in “C:\temp”, and the Product class retrieves the product’s price from the database. Adam has created 2 unit tests, one to check that the Shopping Cart updates its price correctly, and the second one to ensure that logging is done.</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>        [TestMethod]</pre>
<pre><span class="lnum">   2:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> AddProduct_AddingProductWithPrice10_ShouldMakeTotal10()</pre>
<pre class="alt"><span class="lnum">   3:  </span>        {</pre>
<pre><span class="lnum">   4:  </span>            var target = <span class="kwrd">new</span> ShoppingCart(<span class="kwrd">null</span>);</pre>
<pre class="alt"><span class="lnum">   5:  </span>            <span class="rem">//make sure product with ID =1 in database has Price = 10.00</span></pre>
<pre><span class="lnum">   6:  </span>            var product = <span class="kwrd">new</span> Product {ID = 1, Name = <span class="str">"Product1"</span>};</pre>
<pre class="alt"><span class="lnum">   7:  </span>            target.AddProduct(product);</pre>
<pre><span class="lnum">   8:  </span>            Assert.AreEqual(target.Total, 10.00M);</pre>
<pre class="alt"><span class="lnum">   9:  </span>        }</pre>
<pre><span class="lnum">  10:  </span> </pre>
<pre class="alt"><span class="lnum">  11:  </span>        [TestMethod]</pre>
<pre><span class="lnum">  12:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> AddProduct_AddingProduct_ShouldCallLogger()</pre>
<pre class="alt"><span class="lnum">  13:  </span>        {</pre>
<pre><span class="lnum">  14:  </span>            var logger = <span class="kwrd">new</span> Logger();</pre>
<pre class="alt"><span class="lnum">  15:  </span>            var target = <span class="kwrd">new</span> ShoppingCart(logger);</pre>
<pre><span class="lnum">  16:  </span>            var product = <span class="kwrd">new</span> Product {ID = 2, Name = <span class="str">"Product2"</span>};</pre>
<pre class="alt"><span class="lnum">  17:  </span>            target.AddProduct(product);</pre>
<pre><span class="lnum">  18:  </span>            <span class="rem">//Open log file and make sure corresponding log was added.</span></pre>
<pre class="alt"><span class="lnum">  19:  </span>        }</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Clearly, his Unit Tests leave a lot to be desired. They depend on the state of the database and the filesystem and therefore are not fast or in-memory. For the same reasons, they are also not repeatable or automated. These are in fact, more close to Integration Tests than Unit Tests. This is a very common mistake. You advise him to mock his dependencies, and since the ShoppingCart class interacts with interfaces rather than concrete implementations, this should be easy to accomplish. </p>
<table border="1" cellspacing="0" cellpadding="2" bgcolor="#c68158" align="center">
<tbody>
<tr>
<td valign="top" width="967">
<h3 align="center">Stubs VS Mocks</h3>
<p align="left">The definition of stubs and mocks and their differences has been debated many times before. The <a href="http://www.martinfowler.com/articles/mocksArentStubs.html" target="_blank">Mocks Aren’t Stubs</a> article by Martin Fowler is a good resource to get started (The concepts really hit home for me after reading Roy Osherove’s <a href="http://www.amazon.com/Art-Unit-Testing-Examples-Net/dp/1933988274" target="_blank">The Art of Unit Testing</a>. I highly recommend the book if you are getting started with Unit Testing). A few guidelines to remember about Mocks and Stubs are:</p>
<ul>
<li>
<div align="left">You can have many stubs in a unit test, but you should have only one mock object.</div>
</li>
<li>
<div align="left">Stubs help in getting the unit test set up, but you’ll never assert against the stub object. You’ll assert against the class under test.  </div>
</li>
</ul>
<p><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="stub[1]" border="0" alt="stub[1]" src="http://dotnetsurfers.com/blog/images/stub.jpg" width="543" height="312" /></p>
<blockquote>
<p>If you are using mocks, you will assert against the mock object. Stubs lend themselves more naturally to state based unit testing and mocks to interaction based unit testing.</p>
</p>
</blockquote>
<p><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="mock[1]" border="0" alt="mock[1]" src="http://dotnetsurfers.com/blog/images/mock.jpg" width="543" height="312" /></p>
</td>
</tr>
</tbody>
</table>
<p>Adam now understands what he is doing wrong, and works on isolating his unit tests to test only the ShoppingCart class. He creates new implementations for IProduct and ILogger that he can use for Unit Testing. Note that in the new implementations, he only has to put in logic needed to get his unit tests to work.</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> ProductStub:IProduct</pre>
<pre><span class="lnum">   2:  </span>    {</pre>
<pre class="alt"><span class="lnum">   3:  </span>        <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>
<pre><span class="lnum">   4:  </span>        <span class="kwrd">public</span> <span class="kwrd">decimal</span> Price { get; set; }</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> <span class="kwrd">string</span> GetProductCategory()</pre>
<pre><span class="lnum">   6:  </span>        {</pre>
<pre class="alt"><span class="lnum">   7:  </span>            <span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException();</pre>
<pre><span class="lnum">   8:  </span>        }</pre>
<pre class="alt"><span class="lnum">   9:  </span>    }</pre>
<pre><span class="lnum">  10:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> LoggerMock:ILogger</pre>
<pre class="alt"><span class="lnum">  11:  </span>    {</pre>
<pre><span class="lnum">  12:  </span>        <span class="kwrd">public</span> <span class="kwrd">int</span> NumberOfTimesLoggerCalled { get; set; }</pre>
<pre class="alt"><span class="lnum">  13:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Log(<span class="kwrd">string</span> text)</pre>
<pre><span class="lnum">  14:  </span>        {</pre>
<pre class="alt"><span class="lnum">  15:  </span>            NumberOfTimesLoggerCalled++;</pre>
<pre><span class="lnum">  16:  </span>        }</pre>
<pre class="alt"><span class="lnum">  17:  </span>    }</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<p>And he revises his Unit Tests accordingly</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>        [TestMethod]</pre>
<pre><span class="lnum">   2:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> AddProduct_AddingProductWithPrice10_ShouldMakeTotal10()</pre>
<pre class="alt"><span class="lnum">   3:  </span>        {</pre>
<pre><span class="lnum">   4:  </span>            var cart = <span class="kwrd">new</span> ShoppingCart(<span class="kwrd">null</span>);</pre>
<pre class="alt"><span class="lnum">   5:  </span>            var stubProduct = <span class="kwrd">new</span> ProductStub(){Price=10.00M};</pre>
<pre><span class="lnum">   6:  </span>            cart.AddProduct(stubProduct);</pre>
<pre class="alt"><span class="lnum">   7:  </span>            Assert.AreEqual(cart.Total, 10.00M);</pre>
<pre><span class="lnum">   8:  </span>        }</pre>
<pre class="alt"><span class="lnum">   9:  </span> </pre>
<pre><span class="lnum">  10:  </span>        [TestMethod]</pre>
<pre class="alt"><span class="lnum">  11:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> AddProduct_AddingProduct_ShouldCallLogger()</pre>
<pre><span class="lnum">  12:  </span>        {</pre>
<pre class="alt"><span class="lnum">  13:  </span>            var loggerMock = <span class="kwrd">new</span> LoggerMock();</pre>
<pre><span class="lnum">  14:  </span>            var cart = <span class="kwrd">new</span> ShoppingCart(loggerMock);</pre>
<pre class="alt"><span class="lnum">  15:  </span>            <span class="kwrd">int</span> oldNumberOfTimes = loggerMock.NumberOfTimesLoggerCalled;</pre>
<pre><span class="lnum">  16:  </span>            cart.AddProduct(<span class="kwrd">new</span> ProductStub());</pre>
<pre class="alt"><span class="lnum">  17:  </span>            Assert.AreEqual(oldNumberOfTimes + 1, loggerMock.NumberOfTimesLoggerCalled);</pre>
<pre><span class="lnum">  18:  </span>        }</pre>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Adam’s Unit Tests now pass and he has done a good job of keeping them free from external resources such as the filesystem or database by manually mocking his classes. Note how the first Unit Test uses the new IProduct implementation (a Stub) to support the Unit Test but asserts against the Shopping Cart object, whereas the second one asserts against the new ILogger implementation (a Mock).</p>
<p>In the next post, we’ll see how Adam can achieve the same granularity in his Unit Tests by using a mocking framework rather than creating manual mocks.</p>
<p><a href="http://www.dotnetsurfers.com/utils/mocking1.zip" target="_blank">Download Code</a>.</p>
<p><a href="http://www.dotnetsurfers.com/Blog/2010/04/02/GettingStartedWithMockingPart2UsingMOQ.aspx" target="_blank">Part 2</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/02/10/getting-started-with-mocking-part-1-the-basics/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CodeMash 2010: Review</title>
		<link>http://dotnetsurfers.com/blog/2010/01/16/codemash-2010-review/</link>
		<comments>http://dotnetsurfers.com/blog/2010/01/16/codemash-2010-review/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 10:20:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[community]]></category>

		<guid isPermaLink="false">http://www.DotNetSurfers.com/Blog/2010/01/16/CodeMash2010Review.aspx</guid>
		<description><![CDATA[<p>I am sitting at the Cleveland airport right now, waiting for my flight back to Dallas. I spent the last 3 days at the <a href="http://www.codemash.org/" target="_blank">CodeMash</a> conference held at the Kalahari resorts in Sandusky, Ohio. This was the biggest&#8230; <a href="http://dotnetsurfers.com/blog/2010/01/16/codemash-2010-review/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p>I am sitting at the Cleveland airport right now, waiting for my flight back to Dallas. I spent the last 3 days at the <a href="http://www.codemash.org/" target="_blank">CodeMash</a> conference held at the Kalahari resorts in Sandusky, Ohio. This was the biggest (and longest) conference that I have been to. It was exhilarating to be surrounded by and talk to such passionate developers. You could feel the high energy levels throughout the conference. Some of the highlights for me were:</p>
<ul>
<li>Bootstrapping your business session with <a href="http://kohari.org/" target="_blank">Nate Kohari</a> and <a href="http://averyblog.com/" target="_blank">James Avery</a>: I have great respect for entrepreneurs, and more so for those with a coding background. Nate and James shared things that worked for them in their ventures, and a lot of things that didn’t. The session was very interactive, and a great start to the conference for me. </li>
<li>Ruby/IronRuby: One of my goals for going to CodeMash was to decide if I am going to spend any significant amount of time trying to learn Ruby/IronRuby this year. The passion that a lot of the developers (especially the EdgeCase folks)&#160; had for Ruby was definitely contagious and hard to ignore. I ended up attending a couple of Ruby sessions and I hope to spend more time with it in the next couple of months. The best Ruby session for me was “Ruby and Rails for the .Net developer” by <a href="http://blog.mattyoho.com/" target="_blank">Matt Yoho</a>. </li>
<li>Meeting more geeks: I got to meet a bunch of great people I knew from twitter, and some more. </li>
<li>Amazon Ninja puzzles: Amazon had the best vendor booth IMO. They had programming related puzzles for you to solve, and I found myself in front of the question board every few hours (even missed some sessions because of this <img src='http://dotnetsurfers.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . This is me hacking away in the halls with the Amazon Ninjas next to my laptop.<a href="http://www.dotnetsurfers.com/Blog/content/binary/WindowsLiveWriter/CodeMash2010_6FA2/ninja_%5B1%5D_2.jpg"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="ninja_[1]" alt="ninja_[1]" src="http://www.dotnetsurfers.com/blog/images/ninja_.jpg" /></a> </li>
<li>Enter the Haggis concert: There was a live concert by <a href="http://www.enterthehaggis.com/" target="_blank">Enter the Haggis</a> on Thursday night, and they played some great fusion music. This was an awesome way to unwind. <a href="http://www.dotnetsurfers.com/Blog/content/binary/WindowsLiveWriter/CodeMash2010_6FA2/ninja_%5B1%5D_2.jpg"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="enter the haggis" alt="enter the haggis" src="http://www.dotnetsurfers.com/blog/images/haggis_.jpg" /></a> </li>
</ul>
<p>There was plenty of food and caffeine at the conference, though the Wifi could have been better. Thanks to all the organizers for doing such a great job. I hope to be there again next year.</p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/01/16/codemash-2010-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dallas CSharp SIG Presentation Code And Slides</title>
		<link>http://dotnetsurfers.com/blog/2010/01/07/dallas-csharp-sig-presentation-code-and-slides/</link>
		<comments>http://dotnetsurfers.com/blog/2010/01/07/dallas-csharp-sig-presentation-code-and-slides/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 19:42:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MOQ]]></category>
		<category><![CDATA[presentation]]></category>

		<guid isPermaLink="false">http://www.DotNetSurfers.com/Blog/2010/01/07/DallasCSharpSIGPresentationCodeAndSlides.aspx</guid>
		<description><![CDATA[<p>Update: The video for the presentation can be found <a href="http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/01/10/moq-and-pex-dallas-c-sig-on-182010-by.aspx">here.</a>
</p>
<p>I’ll be presenting at the Dallas C# SIG later today, and am uploading a draft of the code and slides, in case somebody wants to work on the&#8230; <a href="http://dotnetsurfers.com/blog/2010/01/07/dallas-csharp-sig-presentation-code-and-slides/" class="read_more">Read the rest</a></p>]]></description>
			<content:encoded><![CDATA[<p>Update: The video for the presentation can be found <a href="http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/01/10/moq-and-pex-dallas-c-sig-on-182010-by.aspx">here.</a>
</p>
<p>I’ll be presenting at the Dallas C# SIG later today, and am uploading a draft of the code and slides, in case somebody wants to work on the code during the presentation. I’ll update the attachment after the presentation if there are any changes.</p>
<p>Download <a href="http://www.dotnetsurfers.com/utils/MOQ_Presentation.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dotnetsurfers.com/blog/2010/01/07/dallas-csharp-sig-presentation-code-and-slides/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 11/21 queries in 0.138 seconds using disk

Served from: dotnetsurfers.com @ 2010-09-08 21:27:46 -->