<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">ploeh blog</title><subtitle type="html">Mark Seemann's thoughts about whatever .NET development topic he's currently immersed in.</subtitle><id>http://blogs.msdn.com/ploeh/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/ploeh/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2008-11-21T13:56:00Z</updated><entry><title>ploeh blog Is Moving</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2009/02/11/ploeh-blog-is-moving.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2009/02/11/ploeh-blog-is-moving.aspx</id><published>2009-02-11T19:01:18Z</published><updated>2009-02-11T19:01:18Z</updated><content type="html">&lt;p&gt;After three years on MSDN blogs, I'll be moving my blog to &lt;a title="http://blog.ploeh.dk" href="http://blog.ploeh.dk"&gt;http://blog.ploeh.dk&lt;/a&gt;. Why? &lt;a href="http://blog.ploeh.dk/2009/01/28/LivingInInterestingTimes.aspx"&gt;Read the answer here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;This is most likely the last post on this blog, so if you'd like to stay tuned, please go and subscribe to the &lt;a href="http://blog.ploeh.dk"&gt;new blog&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9412919" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Miscellaneous" scheme="http://blogs.msdn.com/ploeh/archive/tags/Miscellaneous/default.aspx" /></entry><entry><title>Faking The Windows Azure Table Service</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2009/01/24/faking-the-windows-azure-table-service.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2009/01/24/faking-the-windows-azure-table-service.aspx</id><published>2009-01-24T13:21:36Z</published><updated>2009-01-24T13:21:36Z</updated><content type="html">&lt;p&gt;When presented with a new technology, one of my primary evaluation criteria is its testability, so it should come as no surprise to the regular reader of this blog that this was also foremost in my mind when &lt;a href="http://www.microsoft.com/azure"&gt;Windows Azure&lt;/a&gt; was presented at PDC. Much of the Azure Services Platform is just .NET, so testability is really up to you, just like with all other .NET code.&lt;/p&gt; &lt;p&gt;However, when it comes to the Windows Azure storage, the developer story is different. The Windows Azure Table Service is essentially a REST-based service, and to use it, you'll need to install the Azure Visual Studio SDK.&lt;/p&gt; &lt;p&gt;That's not a problem in itself, but it seems to be implemented as a REST service on top of SQL Server Express. While that may be good enough for casual development, that's quite heavyweight for unit testing. Imagine having to somehow deal with populating and cleaning this database between each test case. That will likely involve a fair amount of complex and undocumented &lt;a href="http://xunitpatterns.com/Back%20Door%20Manipulation.html"&gt;Back Door Manipulation&lt;/a&gt;. As the service is out of process in relation to the test, this is brittle and slow, so a better approach is warranted.&lt;/p&gt; &lt;p&gt;Fortunately, I've already demonstrated &lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx"&gt;how to Fake a REST-based Data Service&lt;/a&gt;, so faking the Windows Azure Table Service turns out to be fairly easy, and generally follows the same steps as previously outlined.&lt;/p&gt; &lt;p&gt;In the following example, I'm working with a simple service that echoes messages back to the caller. The Delay method saves the input message in table storage and returns the &lt;em&gt;previous&lt;/em&gt; message from storage. This test verifies that the input message is being correctly saved:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  DelayWillSaveMessage()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  expectedMessage = \cf6 "Anonymous text"\cf0 ;\par ??\par ??    \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/EchoService"\cf0 );\par ??    \cf3 FakeDataService\cf0 &amp;lt;\cf3 MessageContainer\cf0 &amp;gt; service = \par ??        \cf4 new\cf0  \cf3 FakeDataService\cf0 &amp;lt;\cf3 MessageContainer\cf0 &amp;gt;(\par ??            \cf4 new\cf0  \cf3 MessageContainer\cf0 ());\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??\par ??        \cf3 StorageAccountInfo\cf0  account = \par ??            \cf3 EchoFacadeTest\cf0 .GetAccount();\par ??        \cf3 EchoFacade\cf0  sut = \cf4 new\cf0  \cf3 EchoFacade\cf0 (() =&amp;gt; account);\par ??        \cf5 // Exercise system\par ??\cf0         sut.Delay(expectedMessage);\par ??        \cf5 // Verify outcome\par ??\cf0         \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedMessage,\par ??            service.Container.GetStore&amp;lt;\cf3 FakeMessage\cf0 &amp;gt;().\par ??            First().Message, \cf6 "Delay"\cf0 );\par ??        \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DelayWillSaveMessage()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedMessage = &lt;span style="color: #a31515"&gt;"Anonymous text"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/EchoService"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;FakeDataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;&amp;gt; service = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeDataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;&amp;gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt; host = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt;(service, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { address }))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt; account = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EchoFacadeTest&lt;/span&gt;.GetAccount();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EchoFacade&lt;/span&gt; sut = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;EchoFacade&lt;/span&gt;(() =&amp;gt; account);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Delay(expectedMessage);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedMessage,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; service.Container.GetStore&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt;().&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First().Message, &lt;span style="color: #a31515"&gt;"Delay"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the EchoFacade service, I've used the SDK sample StorageClient library that extents the ADO.NET Data Services Client API to deal with the particulars of Windows Azure Table Storage. One difference from regular ADO.NET Data Services lies in how StorageClient deals with the base URIs of the REST interface. This is a challenge, because a URI like &lt;em&gt;http://localhost/EchoService&lt;/em&gt; is interpreted as a service at the address &lt;em&gt;localhost&lt;/em&gt; belonging to the account &lt;em&gt;EchoService&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This has the implication that I can't just pass the complete address directly to the client API (i.e. EchoFacade), since it would need to decompose it before it can make proper use of it. Fortunately, such a decomposition already exists in the form of the StorageAccountInfo class, so I just need to pass a properly configured instance to my &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; to make it work.&lt;/p&gt;
&lt;p&gt;The test accomplishes this task like this:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 private\cf0  \cf1 static\cf0  \cf4 StorageAccountInfo\cf0  GetAccount()\par ??\{\par ??    \cf4 Uri\cf0  address = \cf1 new\cf0  \cf4 Uri\cf0 (\cf5 "http://localhost/"\cf0 );\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf4 StorageAccountInfo\cf0 (address, \cf1 true\cf0 ,\par ??        \cf5 "EchoService"\cf0 , \cf4 EchoFacadeTest\cf0 .TableKey);\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt; GetAccount()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt;(address, &lt;span style="color: blue"&gt;true&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"EchoService"&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;EchoFacadeTest&lt;/span&gt;.TableKey);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When the service is hosted in the full system (whether in the Development Fabric or on Azure proper), the StorageAccountInfo instance is instead initialized using the static StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration method.&lt;/p&gt;
&lt;p&gt;If you are using the StorageClient library for your data access code, you might as well also take advantage of it when defining the fake service. First of all, you can derive the entities exposed from the Fake service from TableStorageEntity, as this gives you the key properties for free.&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 FakeMessage\cf0  : \cf4 TableStorageEntity\par ??\cf0 \{\par ??    \cf1 public\cf0  FakeMessage()\par ??    \{\par ??        \cf1 this\cf0 .CreationTime = \cf4 DateTime\cf0 .Now;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 DateTime\cf0  CreationTime \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  Message \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;TableStorageEntity&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; FakeMessage()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.CreationTime = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; CreationTime { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; Message { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The DataContainer then becomes equally simple:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 MessageContainer\cf0  : \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 FakeMessage\cf0 &amp;gt; Messages\par ??    \{\par ??        \cf1 get\par ??\cf0         \{ \par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 FakeMessage\cf0 &amp;gt;().\par ??                AsQueryable(); \par ??        \}\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;DataContainer&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt; Messages&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.GetStore&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt;().&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AsQueryable(); &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using a Fake ADO.NET Data Service enables you unit test clients of the Windows Azure Table Service in a light-weight manner.&lt;/p&gt;
&lt;p&gt;In this Fake, I have chosen not to deal with the Windows Azure Table Service's particular way of partitioning and sorting data according to PartitionKey and RowKey. Instead, I just rely on the Fixture of each test to insert the data entities in the expected order.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9373882" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Services" scheme="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx" /></entry><entry><title>A General-Purpose Fake ADO.NET Data Service</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="81103" href="http://blogs.msdn.com/ploeh/attachment/9343868.ashx" /><id>http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx</id><published>2009-01-20T10:10:00Z</published><updated>2009-01-20T10:10:00Z</updated><content type="html">&lt;P&gt;In my &lt;A href="http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx"&gt;previous post&lt;/A&gt;, I discussed how to implement a &lt;A href="http://xunitpatterns.com/Fake%20Object.html" mce_href="http://xunitpatterns.com/Fake%20Object.html"&gt;Fake&lt;/A&gt; ADO.NET Data Service for use with unit testing, showing how you can develop a one-off service that addresses specific needs, such as querying parents and children in the example.&lt;/P&gt;
&lt;P&gt;As I hinted in that post, it's possible to create a reusable Fake ADO.NET Data Service that takes care of implementing &lt;A href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx"&gt;IUpdatable&lt;/A&gt; based on in-memory data storage. To save you the trouble of doing that, I've created one that you can use, if you should so desire.&lt;/P&gt;
&lt;P&gt;Following the standard ADO.NET Data Service coding idiom, the Fake ADO.NET Data Service consists of two classes:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;FakeDataService&amp;lt;T&amp;gt;, which derives from DataService&amp;lt;T&amp;gt;. This is simply the counterpart of MyDataService from the previous example. You can use this class as is. 
&lt;LI&gt;DataContainer, which implements IUpdatable. Although I haven't made this class abstract, it only becomes useful when you derive from it and define your own queries.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Let's say that I want to use these two classes to unit test the same service client as in the previous example. Instead of MyService (where I manually had to implement IUpdatable), I can now define my data container as simply as this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 ParentChildContainer\cf0  : \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; Children\par ??    \{\par ??        \cf1 get\cf0  \par ??        \{\par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 Child\cf0 &amp;gt;().AsQueryable(); \par ??        \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; Parents\par ??    \{\par ??        \cf1 get\par ??\cf0         \{\par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 Parent\cf0 &amp;gt;().AsQueryable(); \par ??        \}\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;DataContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; Children&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetStore&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt;().AsQueryable(); &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetStore&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt;().AsQueryable(); &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The only thing you have to do is just derive from DataContainer and specify your queries.&lt;/P&gt;
&lt;P&gt;Although you don't have to use FakeDataService&amp;lt;T&amp;gt; to host your Fake DataContainer, it makes it easy to set up the Fake service in a unit test:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  TestCanSetupFakeService()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf3 FakeDataService\cf0 &amp;lt;\cf3 ParentChildContainer\cf0 &amp;gt; service = \par ??        \cf4 new\cf0  \cf3 FakeDataService\cf0 &amp;lt;\cf3 ParentChildContainer\cf0 &amp;gt;(\par ??            \cf4 new\cf0  \cf3 ParentChildContainer\cf0 ());\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host =\par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??        \cf5 // Exercise system\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Verify outcome\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; TestCanSetupFakeService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt; address = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"http://localhost/MyDataService"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FakeDataService&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;&amp;gt; service = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FakeDataService&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;&amp;gt;(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WebServiceHost&lt;/SPAN&gt; host =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;WebServiceHost&lt;/SPAN&gt;(service, &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { address }))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;How does this work? DataContainer stores each 'table' in a List&amp;lt;T&amp;gt;, and each list is again stored in a dictionary that maps the list to the type. Here's the part of DataContainer that doesn't contain the IUpdatable implementation:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Dictionary\cf0 &amp;lt;\cf4 Type\cf0 , \cf4 IList\cf0 &amp;gt; stores;\par ??\par ??    \cf1 public\cf0  DataContainer()\par ??    \{\par ??        \cf1 this\cf0 .stores = \cf1 new\cf0  \cf4 Dictionary\cf0 &amp;lt;\cf4 Type\cf0 , \cf4 IList\cf0 &amp;gt;();\par ??    \}\par ??\par ??    [System.Diagnostics.CodeAnalysis.\cf4 SuppressMessage\cf0 (\cf5 "Microsoft.Design"\cf0 , \cf5 "CA1004:GenericMethodsShouldProvideTypeParameter"\cf0 , Justification = \cf5 "As has been discussed on the FxCop DL, this is fair use of generics."\cf0 )]\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;T&amp;gt; GetStore&amp;lt;T&amp;gt;()\par ??    \{\par ??        \cf1 if\cf0  (!\cf1 this\cf0 .stores.ContainsKey(\cf1 typeof\cf0 (T)))\par ??        \{\par ??            \cf1 this\cf0 .AddStore&amp;lt;T&amp;gt;();\par ??        \}\par ??        \cf1 return\cf0  (\cf4 IList\cf0 &amp;lt;T&amp;gt;)\cf1 this\cf0 .stores[\cf1 typeof\cf0 (T)];\par ??    \}\par ??\par ??    \cf1 private\cf0  \cf1 void\cf0  AddStore&amp;lt;T&amp;gt;()\par ??    \{\par ??        \cf1 this\cf0 .stores.Add(\cf1 typeof\cf0 (T), \cf1 new\cf0  \cf4 List\cf0 &amp;lt;T&amp;gt;());\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DataContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;gt; stores;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; DataContainer()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;T&amp;gt; GetStore&amp;lt;T&amp;gt;()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.ContainsKey(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T)))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.AddStore&amp;lt;T&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;T&amp;gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores[&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T)];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; AddStore&amp;lt;T&amp;gt;()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Add(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T), &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;T&amp;gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;When you call GetStore&amp;lt;T&amp;gt;, DataContainer implicitly creates the list for the give type if it doesn't already exist.&lt;/P&gt;
&lt;P&gt;The rest of DataContainer deals with implementing IUpdatable:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 #region\cf0  IUpdatable Members\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  AddReferenceToCollection(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  resourceToBeAdded)\par ??\{\par ??    \cf4 IList\cf0  list = (\cf4 IList\cf0 )targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??    list.Add(resourceToBeAdded);\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  ClearChanges()\par ??\{\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  CreateResource(\cf1 string\cf0  containerName,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf4 Type\cf0  t = \cf1 this\cf0 .ResolveType(fullTypeName);\par ??    \cf1 object\cf0  resource = \cf4 Activator\cf0 .CreateInstance(t);\par ??    \cf1 this\cf0 .stores[t].Add(resource);\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  DeleteResource(\cf1 object\cf0  targetResource)\par ??\{\par ??    \cf1 foreach\cf0  (\cf4 IList\cf0  list \cf1 in\cf0  \cf1 this\cf0 .stores.Values)\par ??    \{\par ??        list.Remove(targetResource);\par ??    \}\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  GetResource(\cf4 IQueryable\cf0  query,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  query.Cast&amp;lt;\cf1 object\cf0 &amp;gt;().AsEnumerable().\par ??        FirstOrDefault();\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  GetValue(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName)\par ??\{\par ??    \cf1 return\cf0  targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  RemoveReferenceFromCollection(\par ??    \cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName,\par ??    \cf1 object\cf0  resourceToBeRemoved)\par ??\{\par ??    \cf4 IList\cf0  list = (\cf4 IList\cf0 )targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??    list.Remove(resourceToBeRemoved);\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  ResetResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  ResolveResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SaveChanges()\par ??\{\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SetReference(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    targetResource.GetType().GetProperty(propertyName).\par ??        SetValue(targetResource, propertyValue, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SetValue(\cf1 object\cf0  targetResource, \par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    targetResource.GetType().GetProperty(propertyName).\par ??        SetValue(targetResource, propertyValue, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 #endregion\par ??\par ??private\cf0  \cf4 Type\cf0  ResolveType(\cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  (\cf1 from\cf0  t \cf1 in\cf0  \cf1 this\cf0 .stores.Keys\par ??            \cf1 where\cf0  t.FullName == fullTypeName\par ??            \cf1 select\cf0  t).First();\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#region&lt;/SPAN&gt; IUpdatable Members&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; AddReferenceToCollection(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resourceToBeAdded)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list = (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Add(resourceToBeAdded);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ClearChanges()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; CreateResource(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; containerName,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; t = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ResolveType(fullTypeName);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource = &lt;SPAN style="COLOR: #2b91af"&gt;Activator&lt;/SPAN&gt;.CreateInstance(t);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores[t].Add(resource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DeleteResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Values)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetResource(&lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt; query,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; query.Cast&amp;lt;&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;().AsEnumerable().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FirstOrDefault();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; RemoveReferenceFromCollection(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resourceToBeRemoved)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list = (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Remove(resourceToBeRemoved);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResetResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResolveResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SaveChanges()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetReference(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource.GetType().GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetValue(targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource.GetType().GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetValue(targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; ResolveType(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; t &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Keys&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; t.FullName == fullTypeName&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; t).First();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;If you find this useful, I've attached the code to this post for your downloading pleasure. As always, the standard disclaimers apply.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9343868" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Services" scheme="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx" /></entry><entry><title>Creating A Fake ADO.NET Data Service</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="47330" href="http://blogs.msdn.com/ploeh/attachment/9339649.ashx" /><id>http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx</id><published>2009-01-19T11:00:00Z</published><updated>2009-01-19T11:00:00Z</updated><content type="html">&lt;P&gt;&lt;A href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx"&gt;Previously&lt;/A&gt;, I discussed unit testing ADO.NET Data Service clients using a &lt;A href="http://xunitpatterns.com/Fake%20Object.html" mce_href="http://xunitpatterns.com/Fake%20Object.html"&gt;Fake&lt;/A&gt; ADO.NET Data Service, and I promised to demonstrate how to create such a service. In this article I will continue the previous example and implement the Fake MyService class.&lt;/P&gt;
&lt;P&gt;The basics of MyService is pretty simple:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 MyService\par ??\cf0 \{\par ??    \cf1 public\cf0  MyService()\par ??    \{\par ??        \cf1 this\cf0 .ParentStore = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt;();\par ??        \cf1 this\cf0 .ChildStore = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt;();\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; Children\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .ChildStore.AsQueryable(); \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; Parents\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .ParentStore.AsQueryable(); \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; ChildStore \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; ParentStore \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; MyService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; Children&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore.AsQueryable(); }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore.AsQueryable(); }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; ChildStore { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; ParentStore { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This service supports two 'tables': Parents and Children (Parents can have Children). Since this is a Fake, it only needs to store its data in memory, so I use two List&amp;lt;T&amp;gt; instances for that. To provide direct access to the service's data, MyService exposes both lists as public properties (you can make those properties internal if your Fake service is defined in the same assembly as your tests).&lt;/P&gt;
&lt;P&gt;This is all you need if you only need to support read-only scenarios. If the client you want to test only queries its service, you don't need to do anything else. On the other hand, if you also need to test Create, Update and Delete (CUD) operations, you will need to implement &lt;A href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx"&gt;IUpdatable&lt;/A&gt;:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 MyService\cf0  : \cf4 IUpdatable}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IUpdatable&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Implementing this interface enables you to support CUD operations. Since it contains twelve members, I'm not going to walk you through each and every one of them, but rather present some highlights. If you are interested in more details on the methods of this interface, I'll recommend &lt;A href="http://blogs.msdn.com/astoriateam/archive/2008/04/10/iupdatable-ado-net-data-services-framework.aspx" mce_href="http://blogs.msdn.com/astoriateam/archive/2008/04/10/iupdatable-ado-net-data-services-framework.aspx"&gt;this post&lt;/A&gt; on the &lt;A href="http://blogs.msdn.com/astoriateam/default.aspx" mce_href="http://blogs.msdn.com/astoriateam/default.aspx"&gt;Project Astoria Team Blog&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;To support each of the simple CUD operations, multiple methods must be implemented, although there's an overlap.&lt;/P&gt;
&lt;P&gt;To create a new item, the first method to implement is CreateResource.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  CreateResource(\cf1 string\cf0  containerName, \par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 switch\cf0  (containerName)\par ??    \{\par ??        \cf1 case\cf0  \cf4 "Parents"\cf0 :\par ??            \cf5 Parent\cf0  p = \cf1 new\cf0  \cf5 Parent\cf0 ();\par ??            \cf1 this\cf0 .ParentStore.Add(p);\par ??            \cf1 return\cf0  p;\par ??        \cf1 case\cf0  \cf4 "Children"\cf0 :\par ??            \cf5 Child\cf0  c = \cf1 new\cf0  \cf5 Child\cf0 ();\par ??            \cf1 this\cf0 .ChildStore.Add(c);\par ??            \cf1 return\cf0  c;\par ??        \cf1 default\cf0 :\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf5 ArgumentException\cf0 (\par ??                \cf4 "Unknown container name."\cf0 );\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; CreateResource(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; containerName, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;switch&lt;/SPAN&gt; (containerName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;case&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"Parents"&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; p = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore.Add(p);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; p;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;case&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"Children"&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt; c = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore.Add(c);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; c;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;default&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #a31515"&gt;"Unknown container name."&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This implementation simply creates a new instance of the requested item type and adds it to the in-memory list. Here, I was just being a bit lazy and simply switched on the containerName, but it's perfectly possible to create a more generic implementation using a bit of Reflection, since the fullTypeName parameter specifies which type of object to create.&lt;/P&gt;
&lt;P&gt;The CreateResource method's responsibility is to create a default instance of the requested type and add it to the underlying data store (in this case just an in-memory list). However, it doesn't assign values to the item's properties, since this is the duty of the SetValue method.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  SetValue(\cf1 object\cf0  targetResource, \par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    \cf4 Type\cf0  t = targetResource.GetType();\par ??    t.GetProperty(propertyName).SetValue(\par ??        targetResource, propertyValue, \cf1 null\cf0 );\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; t = targetResource.GetType();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.GetProperty(propertyName).SetValue(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;At this point, I found it easier to implement the method using Reflection than explicitly switching on both the targetResource and propertyName parameters.&lt;/P&gt;
&lt;P&gt;The last method that must be implemented to support the Create scenario is ResolveResource.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  ResolveResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResolveResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since the Fake service is already working with in-memory objects, it can just return the object itself.&lt;/P&gt;
&lt;P&gt;With these methods implemented, updating an item is not quite as involved, since it reuses the SetValue and ResolveResource methods, so I only need to implement the GetResource method.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  GetResource(\cf4 IQueryable\cf0  query,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  query.Cast&amp;lt;\cf1 object\cf0 &amp;gt;().AsEnumerable().\par ??        FirstOrDefault();\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetResource(&lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt; query,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; query.Cast&amp;lt;&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;().AsEnumerable().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FirstOrDefault();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This implementation requires a bit of explanation. The query argument that the method receives is actually an &lt;EM&gt;expression&lt;/EM&gt; over the service's exposed queries (i.e. Parents and Children). Since the service operates on in-memory objects, it doesn't need to transform the expression, but can simply evaluate it directly, which is what the AsEnumerable extension method does. However, &lt;A href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx"&gt;IQueryable&lt;/A&gt; doesn't have an AsEnumerable extension method, whereas &lt;A href="http://msdn.microsoft.com/en-us/library/bb351562.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb351562.aspx"&gt;IQueryable&amp;lt;T&amp;gt;&lt;/A&gt; does - hence the cast to object.&lt;/P&gt;
&lt;P&gt;Deleting an item reuses GetResource and ResolveResource, so to support this scenario, the DeleteResource method is the only extra method that must be implemented.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  DeleteResource(\cf1 object\cf0  targetResource)\par ??\{\par ??    ((\cf4 IList\cf0 )\cf1 this\cf0 .ParentStore).Remove(targetResource);\par ??    ((\cf4 IList\cf0 )\cf1 this\cf0 .ChildStore).Remove(targetResource);\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DeleteResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore).Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore).Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Once again, I decided to keep things simple and simply hard-code knowledge of ParentStore and ChildStore into the method, but it would be easy to generalize this approach.&lt;/P&gt;
&lt;P&gt;The rest of the methods of IUpdatable address more advanced scenarios concerning references between items, and I'm not going to cover them in this post, since it's already becoming quite long. If you are interested, I've provided the entire sample code as an attachment to this post, so download and peruse it at your leisure (obviously, the usual disclaimers apply).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt; I've now posted &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx"&gt;a general-purpose Fake ADO.NET Data Service&lt;/A&gt; that should hopefully address most of your needs.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9339649" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Services" scheme="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx" /></entry><entry><title>Unit Testing ADO.NET Data Service Clients</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx</id><published>2009-01-14T18:51:13Z</published><updated>2009-01-14T18:51:13Z</updated><content type="html">&lt;p&gt;In my &lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/13/unit-testing-ado-net-data-services.aspx"&gt;previous post&lt;/a&gt;, I discussed unit testing ADO.NET Data Services and how you can host and test the service itself. In this post, I'll take a look at the opposite scenario: You have a client of an ADO.NET Data Service, and you want to unit test the client without relying on the real, production service.&lt;/p&gt; &lt;p&gt;In other words, you want to test the real client against a &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test Double&lt;/a&gt; service. Presently, I'll show you how to create and host a Fake ADO.NET Data Service that the client code can invoke. Obviously, the client in this example uses the ADO.NET Data Services Client API, so I'll demonstrate how you can use that to invoke the fake service from within the same test.&lt;/p&gt; &lt;p&gt;Before we dive into the specifics of the implementation, here's a simple test that verifies that the client can retrieve all Parent instances from the service:&lt;/p&gt; &lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="346" alt="image" src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UnitTestingADO.NETDataServiceClients_A9EC/image_6.png" width="503" border="0"&gt; &lt;/p&gt; &lt;p&gt;As you can see from the figure, the test combines two logical tiers into one test: The service and the client.&lt;/p&gt; &lt;p&gt;The service tier is simply part of the test's &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt;, and while I'm not concerned with testing it in this context, I still need to set it up. This part uses the ADO.NET Data Services API to define and host the service.&lt;/p&gt; &lt;p&gt;The client part uses the ADO.NET Data Services &lt;em&gt;Client&lt;/em&gt; API to query the service via its REST interface and verify the result.&lt;/p&gt; &lt;p&gt;Notice that I use an &lt;em&gt;instance&lt;/em&gt; of MyDataService to configure the host. While not strictly necessary in this test, when hosting a Fake service, it's desirable to have a reference to the fake instance itself, since that allows you to configure and query it through its &lt;a href="http://xunitpatterns.com/back%20door.html"&gt;Back Door&lt;/a&gt;. Here's a test that uses the MyDataService instance to verify that a Parent instance was deleted from the service:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  ClientCanDeleteInstance()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf3 MyDataService\cf0  service = \cf4 new\cf0  \cf3 MyDataService\cf0 ();\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??\par ??        \cf3 MyDataServiceContext\cf0  ctx =\par ??            \cf4 new\cf0  \cf3 MyDataServiceContext\cf0 (address);\par ??        \cf4 var\cf0  victim = (\cf4 from\cf0  p \cf4 in\cf0  ctx.Parents\par ??                      \cf4 where\cf0  p.Id == 3\par ??                      \cf4 select\cf0  p).Single();\par ??        \cf5 // Exercise system\par ??\cf0         ctx.DeleteObject(victim);\par ??        ctx.SaveChanges();\par ??        \cf5 // Verify outcome\par ??\cf0         \cf3 Assert\cf0 .IsFalse(service.Data.Parents.Any(\par ??            p =&amp;gt; p.Id == victim.Id), \cf6 "Deleted"\cf0 );\par ??        \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ClientCanDeleteInstance()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/MyDataService"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt; service = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt; host = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt;(service, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { address }))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyDataServiceContext&lt;/span&gt; ctx =&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataServiceContext&lt;/span&gt;(address);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; victim = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; ctx.Parents&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.Id == 3&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;select&lt;/span&gt; p).Single();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ctx.DeleteObject(victim);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ctx.SaveChanges();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsFalse(service.Data.Parents.Any(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p =&amp;gt; p.Id == victim.Id), &lt;span style="color: #a31515"&gt;"Deleted"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As in the previous example, all &lt;a href="http://xunitpatterns.com/fixture%20setup.html"&gt;Fixture Setup&lt;/a&gt; before the declaration of the MyDataServiceContext instance is part of the logical service tier, while the remaining Fixture Setup code, as well as the execution of the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; is part of the logical client tier. However, notice that the verification now takes place on the logical service tier, since I'm using the fake service instance as a Back Door, instead of relying on the REST interface to query the service.&lt;/p&gt;
&lt;p&gt;Since &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.services.dataservicehost.aspx"&gt;DataServiceHost&lt;/a&gt; doesn't include a constructor that takes a single instance as a parameter, I use its base class &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webservicehost.aspx"&gt;WebServiceHost&lt;/a&gt; instead. &lt;a href="http://www.red-gate.com/products/reflector/"&gt;Reflector&lt;/a&gt; shows that DataServiceHost really adds no behavior to its base class, so using WebServiceHost as a host should be safe - for now (let's hope it stays that way, or even better, that it gets a constructor overload that accepts a service instance).&lt;/p&gt;
&lt;p&gt;In normal cases, I consider it best practice to explicitly load data into the fake service as part of the Fixture Setup phase, but to keep these examples simple, I decided to implicitly load some test data as part of creating the MyDataService instance. That is why, in the above example, I can successfully retrieve a Parent instance with an Id of 3. Please be aware that this is not a strategy I endorse - I only did it to keep the example a bit less complex.&lt;/p&gt;
&lt;p&gt;Here's the MyDataService class in its entirety:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 [\cf3 ServiceBehavior\cf0 (IncludeExceptionDetailInFaults = \cf4 true\cf0 , \par ??    InstanceContextMode = \cf3 InstanceContextMode\cf0 .Single)]\par ??\cf4 public\cf0  \cf4 class\cf0  \cf3 MyDataService\cf0  : \cf3 DataService\cf0 &amp;lt;\cf3 MyService\cf0 &amp;gt;\par ??\{\par ??    \cf4 private\cf0  \cf4 readonly\cf0  \cf3 MyService\cf0  service_;\par ??\par ??    \cf4 public\cf0  MyDataService()\par ??    \{\par ??        \cf4 this\cf0 .service_ = \cf4 new\cf0  \cf3 MyService\cf0 ();\par ??\par ??        \cf3 IList\cf0 &amp;lt;\cf3 Parent\cf0 &amp;gt; ps = \cf4 this\cf0 .service_.ParentStore;\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 1, Text = \cf5 "Ploeh"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 2, Text = \cf5 "Fnaah"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 3, Text = \cf5 "Nd\u248 ?h"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 4, Text = \cf5 "Foo"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 5, Text = \cf5 "Bar"\cf0  \});\par ??\par ??        \cf3 IList\cf0 &amp;lt;\cf3 Child\cf0 &amp;gt; cs = \cf4 this\cf0 .service_.ChildStore;\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 1, Text = \cf5 "Child 1"\cf0  \});\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 2, Text = \cf5 "Child 2"\cf0  \});\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 3, Text = \cf5 "Child 3"\cf0  \});\par ??    \}\par ??\par ??    \cf4 public\cf0  \cf3 MyService\cf0  Data\par ??    \{\par ??        \cf4 get\cf0  \{ \cf4 return\cf0  \cf4 this\cf0 .service_; \}\par ??    \}\par ??\par ??    \cf4 public\cf0  \cf4 static\cf0  \cf4 void\cf0  InitializeService(\par ??        \cf3 IDataServiceConfiguration\cf0  config)\par ??    \{\par ??        config.SetEntitySetAccessRule(\cf5 "*"\cf0 ,\par ??            \cf3 EntitySetRights\cf0 .All);\par ??        config.UseVerboseErrors = \cf4 true\cf0 ;\par ??    \}\par ??\par ??    \cf4 protected\cf0  \cf4 override\cf0  \cf3 MyService\cf0  CreateDataSource()\par ??    \{\par ??        \cf4 return\cf0  \cf4 this\cf0 .service_;\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;ServiceBehavior&lt;/span&gt;(IncludeExceptionDetailInFaults = &lt;span style="color: blue"&gt;true&lt;/span&gt;, &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; InstanceContextMode = &lt;span style="color: #2b91af"&gt;InstanceContextMode&lt;/span&gt;.Single)]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;DataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; service_;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; MyDataService()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_ = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;&amp;gt; ps = &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_.ParentStore;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 1, Text = &lt;span style="color: #a31515"&gt;"Ploeh"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 2, Text = &lt;span style="color: #a31515"&gt;"Fnaah"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 3, Text = &lt;span style="color: #a31515"&gt;"Ndøh"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 4, Text = &lt;span style="color: #a31515"&gt;"Foo"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 5, Text = &lt;span style="color: #a31515"&gt;"Bar"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;&amp;gt; cs = &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_.ChildStore;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 1, Text = &lt;span style="color: #a31515"&gt;"Child 1"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 2, Text = &lt;span style="color: #a31515"&gt;"Child 2"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 3, Text = &lt;span style="color: #a31515"&gt;"Child 3"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; Data&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InitializeService(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IDataServiceConfiguration&lt;/span&gt; config)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.SetEntitySetAccessRule(&lt;span style="color: #a31515"&gt;"*"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EntitySetRights&lt;/span&gt;.All);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.UseVerboseErrors = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; CreateDataSource()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As I described, I use the constructor to set up some implicit test data. While I want to reiterate that this is not something I would recommend, I chose to include this code to make it clear what I was talking about.&lt;/p&gt;
&lt;p&gt;The service is decorated with the &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx"&gt;ServiceBehavior&lt;/a&gt; attribute, which sets its &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.instancecontextmode.aspx"&gt;InstanceContextMode&lt;/a&gt; to &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode.aspx"&gt;Single&lt;/a&gt; to allow it to be hosted as a Singleton service by WebServiceHost. This, again, is what allows me to query the fake service's data via its Back Door.&lt;/p&gt;
&lt;p&gt;Since MyService simply stores data in memory, MyDataService needs to keep the same instance around for each request, so it overrides CreateDataSource to ensure that the same data container instance is always being referenced for the lifetime of the instance.&lt;/p&gt;
&lt;p&gt;In a later post, I will describe how to create the fake data service, MyService.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9318879" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Services" scheme="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx" /></entry><entry><title>Unit Testing ADO.NET Data Services</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2009/01/13/unit-testing-ado-net-data-services.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2009/01/13/unit-testing-ado-net-data-services.aspx</id><published>2009-01-13T15:05:00Z</published><updated>2009-01-13T15:05:00Z</updated><content type="html">&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" mce_href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;ADO.NET Data Services&lt;/A&gt; enables you to expose data (including, but not limited to, relational data) as REST services. Since it's built on top of WCF, it can be tested &lt;A href="http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx"&gt;utilizing similar techniques&lt;/A&gt;, but allow me to elaborate a bit on the subject.&lt;/P&gt;
&lt;P&gt;As with WCF, you might be interested in testing one or both tiers:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The service 
&lt;LI&gt;The client&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Testing the service is the simplest undertaking, so I'll start with that and return to testing the client in a later post.&lt;/P&gt;
&lt;P&gt;Why would you want to unit test an ADO.NET Data Service?&lt;/P&gt;
&lt;P&gt;The mainstream scenario for ADO.NET Data Services is to expose a relational database using an ADO.NET Entity Model. Is it relevant to unit test such a service? Perhaps not. It depends on the amount of code you inject into the generated (partial) classes. If the amount of custom code is minimal, it may not make much sense to test simple CRUD operations.&lt;/P&gt;
&lt;P&gt;Conversely, if you have a greater degree of custom code, or if you implement an ADO.NET Data Service using non-relational data, it makes a lot of sense.&lt;/P&gt;
&lt;P&gt;My loyal readers will probably (correctly) suspect that I would tend to expose a Façade over a Domain Model as a data service, which implies going the non-relational route (even if the underlying data store is, in fact, relational). That is also the context for the rest of this post.&lt;/P&gt;
&lt;P&gt;First of all, you should keep in mind that the data service is just a normal class, so &lt;A href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx"&gt;you can test much of its functionality by just creating it and start using it&lt;/A&gt;:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  ServiceCanReturnRequestedItem()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 int\cf0  anonymousId = 3;\par ??    \cf4 string\cf0  expectedText = \cf6 "Ploeh"\cf0 ;\par ??\par ??    \cf3 Parent\cf0  anonymousParent = \cf4 new\cf0  \cf3 Parent\cf0 ();\par ??    anonymousParent.Id = anonymousId;\par ??    anonymousParent.Text = expectedText;\par ??\par ??    \cf3 MyService\cf0  sut = \cf4 new\cf0  \cf3 MyService\cf0 ();\par ??    sut.ParentStore.Add(anonymousParent);\par ??    \cf5 // Exercise system\par ??\cf0     \cf3 Parent\cf0  result = (\cf4 from\cf0  p \cf4 in\cf0  sut.Parents\par ??                     \cf4 where\cf0  p.Id == anonymousId\par ??                     \cf4 select\cf0  p).Single();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedText, result.Text,\par ??        \cf6 "Text"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ServiceCanReturnRequestedItem()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; anonymousId = 3;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; expectedText = &lt;SPAN style="COLOR: #a31515"&gt;"Ploeh"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; anonymousParent = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; anonymousParent.Id = anonymousId;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; anonymousParent.Text = expectedText;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt; sut = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.ParentStore.Add(anonymousParent);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; result = (&lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; p &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; sut.Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; p.Id == anonymousId&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; p).Single();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedText, result.Text,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #a31515"&gt;"Text"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Notice how most of the test just contains code setting up the &lt;A href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html" mce_href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/A&gt; by adding the relevant data to the service's underlying data store, while execution and verification is quickly accomplished.&lt;/P&gt;
&lt;P&gt;At some point, however, you will probably want to test the service through its REST interface. To do this, you must host the service within the test and then invoke it.&lt;/P&gt;
&lt;P&gt;Hosting the service in a test is easy:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  TestCanHostService()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf4 using\cf0  (\cf3 DataServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 DataServiceHost\cf0 (\cf4 typeof\cf0 (\cf3 MyDataService\cf0 ),\par ??            \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??        \cf5 // Exercise system\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Verify outcome\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; TestCanHostService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt; address = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"http://localhost/MyDataService"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;DataServiceHost&lt;/SPAN&gt; host = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DataServiceHost&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;MyDataService&lt;/SPAN&gt;),&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { address }))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since the service is exposed through a REST interface, you can use more than one technology to invoke it. The ADO.NET Data Services client API is a natural fit, and I'll cover this in a later post. For now, just imagine that the test above contains client code that invokes the service and verifies the result.&lt;/P&gt;
&lt;P&gt;As it turns out, unit testing an ADO.NET Data Service isn't particularly difficult. Unit testing ADO.NET Data Service Clients is a bit more involved, so I'll cover that topic in a separate post. &lt;STRONG&gt;Update:&lt;/STRONG&gt; This post is now available &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9314606" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Services" scheme="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx" /></entry><entry><title>Test-Driven Properties</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2009/01/12/test-driven-properties.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2009/01/12/test-driven-properties.aspx</id><published>2009-01-12T11:26:00Z</published><updated>2009-01-12T11:26:00Z</updated><content type="html">&lt;P&gt;&lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt; post #10:&lt;/P&gt;
&lt;P&gt;In principle, defining properties (or fields, for that matter) while TDD'ing should be no different than &lt;A href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx"&gt;defining methods&lt;/A&gt;. Again, the important part is to stay focused on the test at hand, and not become bogged down by implementation details.&lt;/P&gt;
&lt;P&gt;Unfortunately, neither Visual Studio 2005 nor 2008 has a &lt;EM&gt;Generate property stub&lt;/EM&gt; smart tag, so until the day arrives when we get support for such in our IDE of choice (and for the record: I have no idea about when this is going to happen), we'll have to resort to more primitive means:&lt;/P&gt;
&lt;P&gt;Just reference the non-existing property and move on:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/TestDrivenProperties_1307E/UndefinedProperty_2.png" mce_href="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/TestDrivenProperties_1307E/UndefinedProperty_2.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=139 alt=UndefinedProperty src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/TestDrivenProperties_1307E/UndefinedProperty_thumb.png" width=502 border=0 mce_src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/TestDrivenProperties_1307E/UndefinedProperty_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;As you can see from the picture, my test code gets squiggly lines under the MyProperty property because this property doesn't exist (yet).&lt;/P&gt;
&lt;P&gt;Once more, the important point is to move on and resist the urge to fix this immediately. Don't even waste memory (your own, that is) on the fact that you need to implement this soon; just forget about it right away. The compiler will remind you in due time.&lt;/P&gt;
&lt;P&gt;This technique is a essentially what &lt;A href="http://blog.objectmentor.com/articles/category/michaels-musings" mce_href="http://blog.objectmentor.com/articles/category/michaels-musings"&gt;Michael Feathers&lt;/A&gt; calls &lt;EM&gt;leaning on the compiler&lt;/EM&gt;, although the context is different&lt;EM&gt;.&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9307262" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Productivity" scheme="http://blogs.msdn.com/ploeh/archive/tags/Productivity/default.aspx" /></entry><entry><title>Use The Generate Method Stub Smart Tag To Stay In The Zone</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx</id><published>2009-01-05T11:53:00Z</published><updated>2009-01-05T11:53:00Z</updated><content type="html">&lt;P&gt;Yet Another &lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt; Article (YAZFTA):&lt;/P&gt;
&lt;P&gt;When writing unit tests in the TDD fashion, it's important to stay in the zone and not get side-tracked by irrelevant issues. You need to avoid what I call mental context switching. Focus on writing the test and postpone all else to later, but make sure that you do it in such a way that you don't have to waste brainpower on remembering what you postponed.&lt;/P&gt;
&lt;P&gt;One easy way to do this is by using the &lt;EM&gt;Generate method stub&lt;/EM&gt; smart tag in Visual Studio:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UseTheCreateMethodStubSmartTagToStayInTh_E74B/GenerateMethodStubSmartTag_2.png" mce_href="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UseTheCreateMethodStubSmartTagToStayInTh_E74B/GenerateMethodStubSmartTag_2.png"&gt;&lt;IMG height=125 alt=GenerateMethodStubSmartTag src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UseTheCreateMethodStubSmartTagToStayInTh_E74B/GenerateMethodStubSmartTag_thumb.png" width=515 border=0 mce_src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UseTheCreateMethodStubSmartTagToStayInTh_E74B/GenerateMethodStubSmartTag_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;As we all know, this will generate the following implementation in MyClass:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  MyMethod()\par ??\{\par ??    \cf1 throw\cf0  \cf1 new\cf0  \cf4 NotImplementedException\cf0 ();\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; MyMethod()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NotImplementedException&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;but since you already know that, you don't have to go look at it, and you don't even need to commit to memory that you will have to implement the method. When you run the test, it's going to throw a NotImplementedException, and that's all the reminder you need.&lt;/P&gt;
&lt;P&gt;So just use the smart tag and move on. Right away, forget about the method you just created. Resist the temptation to go implement it immediately, no matter how simple it might be. Stay focused on writing the test.&lt;/P&gt;
&lt;P&gt;You can use the same technique when it comes to evolving &lt;A href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation" mce_href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation"&gt;SUT API Encapsulation&lt;/A&gt;: Sometimes, I know that I'd really like a test helper method that can do something for me, but I have only a vague idea about how to implement it. In such cases, I just write the name of the method, use the smart tag and move on. This helps me stay focused on writing the test, and I can always come back later and implement the helper method at a more convenient time.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9278509" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Productivity" scheme="http://blogs.msdn.com/ploeh/archive/tags/Productivity/default.aspx" /></entry><entry><title>Assert Messages Are Not Optional</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2008/12/17/assert-messages-are-not-optional.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2008/12/17/assert-messages-are-not-optional.aspx</id><published>2008-12-17T15:08:00Z</published><updated>2008-12-17T15:08:00Z</updated><content type="html">&lt;P&gt;...and now, in this week's episode of &lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt;: Optional Assert messages that aren't optional anyway!&lt;/P&gt;
&lt;P&gt;Actually, this piece of advice comes almost directly from the &lt;A href="http://xunitpatterns.com/" mce_href="http://xunitpatterns.com/"&gt;xUnit Test Patterns&lt;/A&gt; book, so I was in doubt whether I should post it all, but it bears repeating, and I guess I still have a few things to add.&lt;/P&gt;
&lt;P&gt;The various &lt;A href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx" mce_href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx"&gt;Assert&lt;/A&gt; methods all have an overload that takes a message string that is basically just echoed to the Test Results list if the test fails, appended to the assertion's more generic message.&lt;/P&gt;
&lt;P&gt;For example, this assertion&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 \cf1 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result, \cf5 "DoStuff"\cf0 );}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedResult, result, &lt;SPAN style="COLOR: #a31515"&gt;"DoStuff"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;will produce this output:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Assert.AreEqual failed. Expected:&amp;lt;ploeh&amp;gt;. Actual:&amp;lt;&amp;gt;. DoStuff&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As there are also overloads available that does &lt;EM&gt;not&lt;/EM&gt; take such a message string, it is tempting to treat it as optional. As far as the compiler is concerned, I might as well have written this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;}??\fs20 \cf1 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result);}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedResult, result);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;which would have yielded this output:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Assert.AreEqual failed. Expected:&amp;lt;ploeh&amp;gt;. Actual:&amp;lt;&amp;gt;.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Not much of a difference, so it may seem quite redundant to supply this extra message.&lt;/P&gt;
&lt;P&gt;Well, not only will I argue that it provides value, I will even go so far as to insist that you should treat it as mandatory. Here's the reason:&lt;/P&gt;
&lt;P&gt;When one or more test failures appear in the Test Results window, my eyes naturally seek out the Error Message text, so it's very nice to have all the information I need present in the same context. All I really need is just a small hint that will jog my memory enough so that I know what I did wrong.&lt;/P&gt;
&lt;P&gt;Please note that I'm not really talking about using the message in the initial Red/Green/Refactor development cycle. In this phase, I rarely read the test failure text at all - I only register that there was a failure, which was expected, since the code isn't implemented yet.&lt;/P&gt;
&lt;P&gt;I'm talking about what happens when you refactor a code base with hundreds of tests. In such cases, I often inadvertently break existing tests, and my eyes immediately seek out the Error Message.&lt;/P&gt;
&lt;P&gt;Imagine that this is the error message:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Assert.AreEqual failed. Expected:&amp;lt;ploeh&amp;gt;. Actual:&amp;lt;&amp;gt;.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;All this really tells me is that in one of my tests (and I don't know which one yet), something was expected to have the value &lt;EM&gt;ploeh&lt;/EM&gt;, but ended up having no value. That's not a lot to go after, so I will probably have to open the test and read it through to get enough context to know what I broke.&lt;/P&gt;
&lt;P&gt;On the other hand, I've experienced that just a &lt;EM&gt;little&lt;/EM&gt; extra message often provides &lt;EM&gt;just enough&lt;/EM&gt; context that I &lt;EM&gt;immediately&lt;/EM&gt; realize what I broke:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Assert.AreEqual failed. Expected:&amp;lt;ploeh&amp;gt;. Actual:&amp;lt;&amp;gt;. DoStuff&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, &lt;EM&gt;DoStuff&lt;/EM&gt; is the name of the method being tested. It may seem hard to believe, but in most cases, this is context enough to help my brain figuring out what went wrong. Keep in mind that the test failure was a result of a code change that &lt;EM&gt;I just implemented&lt;/EM&gt;, so my mind has a lot of context already, and it needs almost no help to make the final connection.&lt;/P&gt;
&lt;P&gt;When I first encountered this guidance, I was sceptic, but after having worked with it for some time, I'm not going back. It has increased my productivity while refactoring, because I don't need to investigate failure causes quite as often as I did before.&lt;/P&gt;
&lt;P&gt;Most developers dislike having to write this optional message because they feel that they ought to write a long piece of fluent prose. That's not the case: Just write a single cue that can trigger your brain at a later date. Normally, I just write the name of the method or property I'm testing - I find that I can easily do that and still stay in the zone when I'm writing my tests.&lt;/P&gt;
&lt;P&gt;You are probably still skeptical, but try it out for a couple of months and see what you think!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9230759" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Productivity" scheme="http://blogs.msdn.com/ploeh/archive/tags/Productivity/default.aspx" /></entry><entry><title>First Dynamics Mobile Post</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2008/12/12/first-dynamics-mobile-post.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2008/12/12/first-dynamics-mobile-post.aspx</id><published>2008-12-12T18:00:51Z</published><updated>2008-12-12T18:00:51Z</updated><content type="html">&lt;p&gt;Yesterday, I posted my &lt;a href="http://blogs.msdn.com/dynamicsmobile/archive/2008/12/11/documenthandler-behind-the-scenes.aspx"&gt;first post&lt;/a&gt; over on the &lt;a href="http://blogs.msdn.com/dynamicsmobile/default.aspx"&gt;Microsoft Dynamics Mobile Team blog&lt;/a&gt;. More are likely to follow.&lt;/p&gt; &lt;p&gt;In case you are wondering what this means for &lt;em&gt;this&lt;/em&gt; blog, the answer is: Nothing. I plan to continue blogging in more or less the same vein as I've done for the last three years now.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9202399" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Miscellaneous" scheme="http://blogs.msdn.com/ploeh/archive/tags/Miscellaneous/default.aspx" /></entry><entry><title>Why Use AreEqual&lt;T&gt;?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2008/12/11/why-use-areequal-t.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2008/12/11/why-use-areequal-t.aspx</id><published>2008-12-11T11:55:00Z</published><updated>2008-12-11T11:55:00Z</updated><content type="html">&lt;P&gt;This is a post in my &lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt; series.&lt;/P&gt;
&lt;P&gt;One of my colleagues recently asked my why I prefer &lt;A href="http://msdn.microsoft.com/en-us/library/ms243446.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms243446.aspx"&gt;Assert.AreEqual&amp;lt;T&amp;gt;&lt;/A&gt; over one of the non-generic &lt;A href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.areequal.aspx" mce_href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.areequal.aspx"&gt;Assert.AreEqual&lt;/A&gt; overloads for primitive types.&lt;/P&gt;
&lt;P&gt;In most cases, I'm very happy with the C# compiler's ability to infer generic type arguments from the method call parameters, but this case is a little different.&lt;/P&gt;
&lt;P&gt;Let's say you want to compare two strings. I always do it like this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 \cf1 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result, \cf5 "DoStuff"\cf0 );}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedResult, result, &lt;SPAN style="COLOR: #a31515"&gt;"DoStuff"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This statement explicitly tells the compiler to use AreEqual&amp;lt;string&amp;gt;. However, if I omit the type parameter, type inferencing is &lt;EM&gt;not&lt;/EM&gt; going to happen:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;}??\fs20 \cf1 Assert\cf0 .AreEqual(expectedResult, result, \cf4 "DoStuff"\cf0 );}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual(expectedResult, result, &lt;SPAN style="COLOR: #a31515"&gt;"DoStuff"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Instead of inferring the type parameter from the parameters, the compiler (correctly) selects the best matching overload, in this case, &lt;A href="http://msdn.microsoft.com/en-us/library/ms243496.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms243496.aspx"&gt;AreEqual(object, object, string)&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;When both the &lt;EM&gt;expectedResult&lt;/EM&gt; and &lt;EM&gt;result&lt;/EM&gt; variables are strings, this will still compile and work, but you just lost static type checking.&lt;/P&gt;
&lt;P&gt;There are 18 overloads of AreEqual, but that still leaves many combinations where type inferencing will occur. Consider comparing two integers:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 \cf1 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedNumber, result, \cf5 "Explicit typing"\cf0 );}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;(expectedNumber, result, &lt;SPAN style="COLOR: #a31515"&gt;"Explicit typing"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since there's no non-generic overload that takes Int32 instances, you could omit the type parameter and still arrive at the AreEqual&amp;lt;int&amp;gt; overload:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;}??\fs20 \cf1 Assert\cf0 .AreEqual(expectedNumber, result, \cf4 "Implicit typing"\cf0 );}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual(expectedNumber, result, &lt;SPAN style="COLOR: #a31515"&gt;"Implicit typing"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;However, what happens if you change the type of &lt;EM&gt;result&lt;/EM&gt; to a string? In the first case, you will get a compilation error, but in the latter case, the compiler instead selects the AreEqual(object, object, string) overload!&lt;/P&gt;
&lt;P&gt;When you perform refactorings that change the type of one of the assertion variables, the tests will still compile if you rely on type inferencing, but may fail at run-time. With the refactoring support in Visual Studio, you may be changing the type of a property somewhere else in your code base without thinking about the test code at all. If you test directly against such a property in your AreEqual assertion, static type checking could protect you, but the non-generic overloads may introduce subtle errors in your tests.&lt;/P&gt;
&lt;P&gt;Since test suites should be executed often, you might argue that you'll discover errors like that soon enough, but I'm a strong believer in &lt;EM&gt;fail fast&lt;/EM&gt;, so I still rather prefer getting a compiler error than a run-time error in my test suite.&lt;/P&gt;
&lt;P&gt;The easiest strategy is to not think about this at all, and just always explicitly define the type, so that's what I do: I always use AreEqual&amp;lt;T&amp;gt; and &lt;A href="http://msdn.microsoft.com/en-us/library/ms243707.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms243707.aspx"&gt;AreNotEqual&amp;lt;T&amp;gt;&lt;/A&gt; (except in special cases, but more about that later).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9195808" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Productivity" scheme="http://blogs.msdn.com/ploeh/archive/tags/Productivity/default.aspx" /></entry><entry><title>3 Is Many</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx</id><published>2008-12-08T17:51:00Z</published><updated>2008-12-08T17:51:00Z</updated><content type="html">&lt;P&gt;This is an installment in my &lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt; series.&lt;/P&gt;
&lt;P&gt;When I was a kid, my parents taught me that &lt;EM&gt;many&lt;/EM&gt; is any number above &lt;STRIKE&gt;three&lt;/STRIKE&gt; two*; they used the simple counting sequence &lt;EM&gt;one, two, many&lt;/EM&gt;. This little story may make me seem like I was an incredibly dim-witted kid, but the point was obviously not that I couldn't count to more than three, but rather that, in many cases, unary and binary systems may be special, but as soon as you cross the threshold to three, you are dealing with a number that doesn't exhibit any particular characteristics.&lt;/P&gt;
&lt;P&gt;Note: I'm not talking about number theory here. I'm sure someone will be able to tell me about all sorts of special characteristics of the number 3. Incidentally, three also seems to be a magic number in many cults and religions. That's not what I'm talking about either.&lt;/P&gt;
&lt;P&gt;If you will allow me to further digress, studies have shown that some animals can count to both three and also higher numbers. This goes for humans as well: If I place a number of marbles/dice/glass beads/whatever in front of you, and you have to give me the number as fast as possible, you can probably do that &lt;EM&gt;without&lt;/EM&gt; counting up to perhaps five or six. For higher numbers, you will have to count the items. Three is an instantly recognizable number by the human mind, but I'm not talking about cognition either.&lt;/P&gt;
&lt;P&gt;As a colleague once told me: &lt;EM&gt;The hardest scale-out you can perform is going from one to two&lt;/EM&gt;. Nonetheless, if you look at concepts such as active/passive failover clustering, it would seem as though going from two to three is also non-trivial.&lt;/P&gt;
&lt;P&gt;However, once you have managed to deal with three of whatever it is you deal with, you are probably ready to deal with four, five, or even twenty-seven. To bring the topic back to code, at that point you are probably working with a list of some sort.&lt;/P&gt;
&lt;P&gt;When testing against lists, 3 is a good &lt;A href="http://xunitpatterns.com/equivalence%20class.html" mce_href="http://xunitpatterns.com/equivalence%20class.html"&gt;Equivalence Class&lt;/A&gt; for &lt;EM&gt;many&lt;/EM&gt;. If you have a list of three elements, you have distinct elements for the head, interior and tail of the list. You can perform meaningful sorts and filtering on such a list.&lt;/P&gt;
&lt;P&gt;In fact, I usually find that I can reproduce most scenarios (as well as most bugs) with lists of three elements.&lt;/P&gt;
&lt;P&gt;When writing unit tests, for maintainability the number 3 shouldn't just appear as a magic number in the test, so it makes more sense to define it as a constant somewhere, as in this, otherwise inane, example:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 \cf1 private\cf0  \cf1 const\cf0  \cf1 int\cf0  Many = 3;\par ??\par ??[\cf4 TestMethod\cf0 ]\par ??\cf1 public\cf0  \cf1 void\cf0  SumOfManyNumbersWillBeCorrect()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 IEnumerable\cf0 &amp;lt;\cf1 int\cf0 &amp;gt; anonymousNumbers = \par ??        \cf4 Enumerable\cf0 .Range(0, \cf4 MyClassTest\cf0 .Many);\par ??    \cf1 int\cf0  expectedSum = anonymousNumbers.Sum();\par ??    \cf4 MyClass\cf0  sut = \cf1 new\cf0  \cf4 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf1 int\cf0  result = sut.Sum(anonymousNumbers);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf4 Assert\cf0 .AreEqual&amp;lt;\cf1 int\cf0 &amp;gt;(expectedSum, result, \cf6 "Sum"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;const&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; Many = 3;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SumOfManyNumbersWillBeCorrect()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt; anonymousNumbers = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Enumerable&lt;/SPAN&gt;.Range(0, &lt;SPAN style="COLOR: #2b91af"&gt;MyClassTest&lt;/SPAN&gt;.Many);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; expectedSum = anonymousNumbers.Sum();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt; sut = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; result = sut.Sum(anonymousNumbers);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;(expectedSum, result, &lt;SPAN style="COLOR: #a31515"&gt;"Sum"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The only noticeable part of this piece of code is the Many constant, which is used in the test code itself. This is a good hint to the &lt;A href="http://xunitpatterns.com/test%20reader.html" mce_href="http://xunitpatterns.com/test%20reader.html"&gt;Test Reader&lt;/A&gt; that this test deals with many integers, without particularly caring about how many there are.&lt;/P&gt;
&lt;P&gt;* Edit 2009.01.20&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9184791" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Productivity" scheme="http://blogs.msdn.com/ploeh/archive/tags/Productivity/default.aspx" /></entry><entry><title>Creating Azure Tables From Script</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2008/12/04/creating-azure-tables-from-script.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2008/12/04/creating-azure-tables-from-script.aspx</id><published>2008-12-04T15:04:00Z</published><updated>2008-12-04T15:04:00Z</updated><content type="html">&lt;P&gt;When working with the &lt;A href="http://www.microsoft.com/azure/default.mspx" mce_href="http://www.microsoft.com/azure/default.mspx"&gt;Windows Azure&lt;/A&gt; Storage service, you must create the tables before you can use them; in essence, defining the 'schema' of your Azure storage service.&lt;/P&gt;
&lt;P&gt;On his blog, &lt;A href="http://blog.smarx.com/" mce_href="http://blog.smarx.com/"&gt;Steve Marx&lt;/A&gt; &lt;A href="http://blog.smarx.com/posts/try-to-create-tables-only-once" mce_href="http://blog.smarx.com/posts/try-to-create-tables-only-once"&gt;writes&lt;/A&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;"Probably the best solution is to have separate initialization code that creates your tables.&amp;nbsp; This is analogous to the pattern of having &lt;CODE&gt;CREATE TABLE&lt;/CODE&gt; commands scripted in T-SQL which you run once to set up the database."&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I can only agree, but if you are relying on the CreateTablesFromModel method from the StorageClient data access API (from the Azure SDK), how can you do that?&lt;/P&gt;
&lt;P&gt;The first thing to realize is that since you can connect to your Azure Storage Service from anywhere, &lt;EM&gt;the script does not need to execute in the cloud&lt;/EM&gt;. You can just as well run this script from your local development machine, as long as you can connect to your storage account.&lt;/P&gt;
&lt;P&gt;You could obviously write a little utility that references StorageClient and your custom TableStorageDataServiceContext.&lt;/P&gt;
&lt;P&gt;Another, in my opinion, better option for such a one-off script is a PowerShell script. Here's my first take on such a script:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;}??\fs20 $address = "http://table.core.windows.net"\par ??$account = "mbis"\par ??$accessKey = "SJ1EqZCUGoVejikA9GdPbMv+IAfpjDI9iOGGfAOMT0NPqXcheT+Vt+RS2o0N04lUG3rEna04+eRbDoJU4ocPOQ=="\par ??\par ??$storageClientPath = "C:\\MAG1_MBIS\\Spikes\\AzureServer\\Document Service\\MobileServerAzureDataAccess\\bin\\Debug\\StorageClient.dll"\par ??$contextPath = "C:\\MAG1_MBIS\\Spikes\\AzureServer\\Document Service\\MobileServerAzureDataAccess\\bin\\Debug\\Microsoft.Dynamics.Mobile.Server.AzureDataAccess.dll"\par ??\par ??[System.Reflection.Assembly]::LoadFrom($storageClientPath)\par ??[System.Reflection.Assembly]::LoadFrom($contextPath)\par ??\par ??$ai = New-Object Microsoft.Samples.ServiceHosting.StorageClient.StorageAccountInfo($address, $null, $account, $accessKey)\par ??\par ??$dataContext = New-Object Microsoft.Dynamics.Mobile.Server.AzureDataAccess.MobileServerDataServiceContext($ai)\par ??$t = $dataContext.GetType()\par ??\par ??$ts = [Microsoft.Samples.ServiceHosting.StorageClient.TableStorage]::Create($ai)\par ??$ts.ListTables() | % \{ $ts.DeleteTable($_) \}\par ??\par ??[Microsoft.Samples.ServiceHosting.StorageClient.TableStorage]::CreateTablesFromModel($t, $ai)\par ??}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;$address = "http://table.core.windows.net"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$account = &amp;lt;accountName&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$accessKey = &amp;lt;accessKey&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$storageClientPath = &amp;lt;storageClientPath&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$contextPath = &amp;lt;contextPath&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[System.Reflection.Assembly]::LoadFrom($storageClientPath)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[System.Reflection.Assembly]::LoadFrom($contextPath)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$ai = New-Object Microsoft.Samples.ServiceHosting.StorageClient.StorageAccountInfo($address, $null, $account, $accessKey)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$dataContext = New-Object &amp;lt;customContext&amp;gt;($ai)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$t = $dataContext.GetType()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$ts = [Microsoft.Samples.ServiceHosting.StorageClient.TableStorage]::Create($ai)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$ts.ListTables() | % { $ts.DeleteTable($_) }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[Microsoft.Samples.ServiceHosting.StorageClient.TableStorage]::CreateTablesFromModel($t, $ai)&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;First of all, you will need to fill in the values that I've enclosed in angle brackets (remember quotes):&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;accountName&lt;/STRONG&gt; is the name of your Windows Azure Storage account &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;accessKey&lt;/STRONG&gt; is your Windows Azure Storage access key &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;storageClientPath&lt;/STRONG&gt; is the full path to the StorageClient assembly, e.g. "C:\Program Files\Windows Azure SDK\v1.0\Samples\StorageClient\Lib\bin\Debug\StorageClient.dll" &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;contextPath&lt;/STRONG&gt; is the full path to the assembly that contains your custom TableStorageDataServiceContext&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;customContext&lt;/STRONG&gt; is the fully qualified type name of your custom context (see the update comment below)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;This script does the following:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;It loads the StorageClient and your custom assembly into memory so types from those assemblies can be consumed by the script. &lt;/LI&gt;
&lt;LI&gt;It loops through all the tables currently in storage and &lt;EM&gt;deletes&lt;/EM&gt; them (delete the penultimate line if you don't want it to do this)! &lt;/LI&gt;
&lt;LI&gt;It creates the tables from your data model. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Currently, the script has one limitation: Deleting a table using the StorageClient API only marks the table for deletion, so the operation returns much to soon. This means that if you are trying to recreate a table by the same name, a conflict will occur, and the table will not be created. You can work around this limitation by waiting a little while and then run the script again.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt; In the original version of this post, I accidentally forgot to remove the specific type name of the custom context class that I'd used to create the script. Since you will not have access to that class, it doesn't make sense, so I replaced it with the &lt;EM&gt;customContext&lt;/EM&gt; placeholder.&lt;/P&gt;
&lt;P&gt;In case you were wondering, the original line of code was:&lt;/P&gt;
&lt;P&gt;$dataContext = New-Object Microsoft.Dynamics.Mobile.Server.AzureDataAccess.MobileServerDataServiceContext($ai)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9174903" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Services" scheme="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx" /></entry><entry><title>testmethod Code Snippet</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2008/12/01/testmethod-code-snippet.aspx" /><link rel="enclosure" type="text/xml" length="885" href="http://blogs.msdn.com/ploeh/attachment/9160423.ashx" /><id>http://blogs.msdn.com/ploeh/archive/2008/12/01/testmethod-code-snippet.aspx</id><published>2008-12-01T16:32:00Z</published><updated>2008-12-01T16:32:00Z</updated><content type="html">&lt;P&gt;This is an installment in my &lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt; series.&lt;/P&gt;
&lt;P&gt;If you are a regular reader of this blog, you may have noticed a certain pattern in my unit test examples (like &lt;A href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;this one&lt;/A&gt;). This is because I always follow the &lt;A href="http://xunitpatterns.com/Four%20Phase%20Test.html" mce_href="http://xunitpatterns.com/Four%20Phase%20Test.html"&gt;Four-Phase Test&lt;/A&gt; pattern (which is a superset of the more well-know Triple-A (Arrange Act Assert) pattern), with a few code comments to denote the different phases (incidentally, in this case, I don't regard these code comments as &lt;A href="http://www.butunclebob.com/ArticleS.TimOttinger.ApologizeIncode" mce_href="http://www.butunclebob.com/ArticleS.TimOttinger.ApologizeIncode"&gt;Apologies&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;Writing the skeleton of such a test (the TestMethod attribute, the method declaration, the four comments) is very repetitious work, and I got tired of it around the third or fourth time I had to do it. Therefore, I wrote a code snippet that, in essence, just generates this template:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  Test()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf5 // Exercise system\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .Inconclusive();\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Test()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.Inconclusive();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;From there, you are ready to go ahead and fill in the blanks. It saves me a bit of typing for &lt;EM&gt;every&lt;/EM&gt; test I write and allows me to get started faster with formulating the actual value-adding code, while the structure reminds me to write good and maintainable tests.&lt;/P&gt;
&lt;P&gt;I've been using this code snippet for more than a year now, and although I've considered many enhancements to it, I've always felt comfortable with the versatility that this simple implementation provides.&lt;/P&gt;
&lt;P&gt;For your convenience, I've attached the code snippet to this post: Just download it and put it in your &amp;lt;Documents&amp;gt;\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets folder. Now you can just type &lt;EM&gt;testmethod&lt;/EM&gt; and expand the snippet, and you have a structured Four-Phase Test ready to be implemented.&lt;/P&gt;
&lt;P&gt;Once more, it saves you a bit of mental context switching, and you can focus on writing the test code that really matters.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9160423" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Productivity" scheme="http://blogs.msdn.com/ploeh/archive/tags/Productivity/default.aspx" /></entry><entry><title>Ignore Irrelevant Return Values</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/ploeh/archive/2008/11/21/ignore-irrelevant-return-values.aspx" /><id>http://blogs.msdn.com/ploeh/archive/2008/11/21/ignore-irrelevant-return-values.aspx</id><published>2008-11-21T15:56:00Z</published><updated>2008-11-21T15:56:00Z</updated><content type="html">&lt;P&gt;This is an installment in my &lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt; series.&lt;/P&gt;
&lt;P&gt;Sometimes, you don't care about the return value from a particular operation. The simplest example is if you want to check that creating a new instance of a specific type will throw an exception if supplied with wrong parameter values:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 ExpectedException\cf0 (\cf4 typeof\cf0 (\cf3 ArgumentNullException\cf0 ))]\par ??[\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  CreateWithNullTextWillThrow()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 int\cf0  anonymousNumber = 8;\par ??    \cf4 string\cf0  nullText = \cf4 null\cf0 ;\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 new\cf0  \cf3 Plop\cf0 (anonymousNumber, nullText);\par ??    \cf5 // Verify outcome (expected exception)\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;ExpectedException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;ArgumentNullException&lt;/SPAN&gt;))]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; CreateWithNullTextWillThrow()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; anonymousNumber = 8;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; nullText = &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Plop&lt;/SPAN&gt;(anonymousNumber, nullText);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome (expected exception)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;In this example, even though the &lt;EM&gt;new&lt;/EM&gt; operation is supposed to return a value, we don't care about it, since we only want to test that the correct exception type is being thrown. To save myself from having to stop and think about a variable name (even though in this case, &lt;A href="http://blogs.msdn.com/ploeh/archive/2008/10/06/naming-sut-test-variables.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/10/06/naming-sut-test-variables.aspx"&gt;I'd just have used &lt;EM&gt;sut&lt;/EM&gt;&lt;/A&gt;), I prefer to not declare and assign the variable at all.&lt;/P&gt;
&lt;P&gt;In general, I believe that APIs should follow the principle of Command-Query Separation, but sometimes it makes sense to break this rule. When you have an operation that both return a value and have side-effects, testing the side-effect via state-based testing doesn't require the return value.&lt;/P&gt;
&lt;P&gt;Imaging that you have a method with this signature:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  CommandQuery(\cf1 int\cf0  number)}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; CommandQuery(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; number)&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Testing the side-effect may look like this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  CommandQueryWillUpdateNumberProperty()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 int\cf0  expectedNumber = 89;\par ??    \cf4 int\cf0  anonymousNumber = 11;\par ??    \cf4 string\cf0  anonymousText = \cf6 "Anonymous text"\cf0 ;\par ??    \cf3 Plop\cf0  sut = \cf4 new\cf0  \cf3 Plop\cf0 (anonymousNumber, anonymousText);\par ??    \cf5 // Exercise system\par ??\cf0     sut.CommandQuery(expectedNumber);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedNumber, sut.Number, \cf6 "CommandQuery"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; CommandQueryWillUpdateNumberProperty()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; expectedNumber = 89;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; anonymousNumber = 11;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; anonymousText = &lt;SPAN style="COLOR: #a31515"&gt;"Anonymous text"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Plop&lt;/SPAN&gt; sut = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Plop&lt;/SPAN&gt;(anonymousNumber, anonymousText);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.CommandQuery(expectedNumber);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;(expectedNumber, sut.Number, &lt;SPAN style="COLOR: #a31515"&gt;"CommandQuery"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Notice that I've ignored the return value of the CommandQuery method.&lt;/P&gt;
&lt;P&gt;Save yourself the time and effort it takes to declare and assign variables if you don't use them anyway.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9131791" width="1" height="1"&gt;</content><author><name>ploeh</name><uri>http://blogs.msdn.com/members/ploeh.aspx</uri></author><category term="Unit Testing" scheme="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx" /><category term="Productivity" scheme="http://blogs.msdn.com/ploeh/archive/tags/Productivity/default.aspx" /></entry></feed>