<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://agileisrael.org/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'Agile Related' and 'Testing Guidelines'</title><link>http://agileisrael.org/cs/search/SearchResults.aspx?o=DateDescending&amp;tag=Agile+Related,Testing+Guidelines&amp;orTags=0</link><description>Search results matching tags 'Agile Related' and 'Testing Guidelines'</description><dc:language>en-US</dc:language><generator>CommunityServer 2007 (Build: 20416.853)</generator><item><title>Unit Testing Denial Pattern #1: Pretending the test is no longer valid</title><link>http://agileisrael.org/cs/blogs/royo/archive/2008/04/30/unit-testing-denial-pattern-1-pretending-the-test-is-no-longer-valid.aspx</link><pubDate>Wed, 30 Apr 2008 20:21:26 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:1529</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;Today was both a good day for e and a bad day for me. Good day because I managed to get a full suite of tests that were somehow failing to work again after a refactoring that got out of hand. Bad - because out of all the tests, one of them failed. &lt;/p&gt;  &lt;p&gt;Here&amp;#39;s the part I&amp;#39;m ashamed of:&lt;/p&gt;  &lt;p&gt;I was working with a pair, and when we both looked at the failing test, I went out and said &amp;quot;Oh, I think this test is no longer valid. It may just need to be removed&amp;quot;. We both knew it was still a valid test, that for some reason was failing. But I was on such a role with making everything work, that test was feeling like a thorn in my thumb that I wanted to get rid of. &lt;/p&gt;  &lt;p&gt;It was a hard few minutes to convince ourselves to debug through the test and realize what the problem may be&lt;/p&gt;  &lt;p&gt;So, denial pattern #1:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Forces&lt;/strong&gt;: You just want to get on with your code, with only one or two tests that are &amp;quot;ruining the party&amp;quot;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Behavior:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Instead of fixing them debugging them or accepting that you might not have fixed everything, you try to tell yourself that the tests may no longer be valid without actually checking your assumption, which you really wish would be true. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;How to avoid&lt;/strong&gt;: work in pairs. It&amp;#39;s harder to ignore a problem (or at least more awkward) with 4 eyes staring at it.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6145763" width="1" height="1" alt="" /&gt;</description></item><item><title>Basic Guidelines for using RowTest and data driven tests</title><link>http://agileisrael.org/cs/blogs/royo/archive/2007/12/18/basic-guidelines-for-using-rowtest-and-data-driven-tests.aspx</link><pubDate>Tue, 18 Dec 2007 23:28:32 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:742</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;I took a look at the code that was used to create the &lt;a href="http://www.andreas-schlapsi.com/projects/rowtest-extension-for-nunit/"&gt;NUnit RowTest Extension&lt;/a&gt; (which is pretty neat) and found this in one of the samples:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/a177d915a4ab_1383/image_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="359" alt="image" src="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/a177d915a4ab_1383/image_thumb.png" width="591" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The highlighted areas in the code show something which I feel is problematic and is the manifestation of a unit test anti-pattern which the XUnit book calls &amp;quot;Interacting tests&amp;quot;. basically, there is a dependency that is created by the first and second row, in which the second row cannot run individually. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Bad Naming once again leads to confusion&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I&amp;#39;m not saying that this test is bad, because perhaps that is what they author was &lt;strong&gt;trying&lt;/strong&gt; to test - that the class level members are not destroyed between each row. I guess we&amp;#39;ll never know because the naming of the test is problematic - it doesn&amp;#39;t say what the expected behavior is, or what the scenario\context is. just what the general object being test is (and I&amp;#39;m not even sure about that). Still, it makes for a nice example for your own tests.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The symptoms of interacting row tests&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Back to the issue at hand, the symptoms detailed below are caused by the wrong usage (in my mind) of the RowTest facility. The second argument in the RowTest is provided for the sole purpose of changing the behavior of the class under test (changing s_classMember) so that the &lt;strong&gt;next&lt;/strong&gt; row will be able to assert on the string.&lt;/p&gt;  &lt;p&gt;Now this prevents you from doing the following without going into annoying routines of code replacements:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;You can&amp;#39;t remove a row test without making sure you don&amp;#39;t break any other rows&lt;/li&gt;    &lt;li&gt;You can&amp;#39;t just insert a row test randomly in between without possible breaking another row&lt;/li&gt;    &lt;li&gt;You have to read the code in the test to understand what the row test is doing.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Introducing dependencies between row test essentially makes the specific test method unmaintainable to a large degree. Since row-tests and data driven tests are a fairly new syntax for most of us, here are a few guidelines I try to follow:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Some Basic Guidelines&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Treat each test row as if it will be the only one on the test method (so that you don&amp;#39;t feel the need to interact with other rows).&lt;/li&gt;    &lt;li&gt;Test rows must be a simple as possible to understand - that is - have no logic. In this case we &lt;strong&gt;do&lt;/strong&gt; have logic int he test row (pertaining to the next row)&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5470426" width="1" height="1" alt="" /&gt;</description></item><item><title>Data Layer Testing: Test Inheritance Patterns</title><link>http://agileisrael.org/cs/blogs/royo/archive/2007/11/28/data-layer-testing-test-inheritance-patterns.aspx</link><pubDate>Thu, 29 Nov 2007 01:56:51 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:675</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;I&amp;#39;m preparing for my tutorial on Database and Data Layer unit testing that I will be giving at the Agile Practices Conference&amp;#160; next week. It&amp;#39;s always nice to have a full 3 hours to get pretty deep into something. 75 minutes just isn&amp;#39;t enough to grasp a concept deeply enough, just to graze it around the edges.&lt;/p&gt;  &lt;p&gt;When writing data layer tests, there are several patterns I&amp;#39;ve noticed, especially around the notion of inheritance within the test classes (Abstract test class pattern).&amp;#160; I&amp;#39;ve observed at least three different patterns emerging from inheritance that I&amp;#39;ve personally used, all relating to data layer testing, but could be used elsewhere:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Abstract Utility Class&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/DataLayerTestingTestInheritancePatterns_3770/image_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 10px 0px 0px;border-left:0px;border-bottom:0px;" height="243" alt="image" src="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/DataLayerTestingTestInheritancePatterns_3770/image_thumb.png" width="244" align="left" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt; This class will usually be a base class for tests written for data layer objects. it would contain &amp;quot;facilities&amp;quot; such as database helper classes, setup and teardown methods that would automatically rollback any test and anything else a derived base class that was testing a data layer object would need (helper methods for asserting complex stuff, for example).&lt;/p&gt;  &lt;p&gt;This is a classic &amp;quot;Abstract test class&amp;quot; pattern, and the next two are built on top of it.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Template Test Class&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/DataLayerTestingTestInheritancePatterns_3770/image_4.png"&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 10px 0px 0px;border-left:0px;border-bottom:0px;" height="227" alt="image" src="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/DataLayerTestingTestInheritancePatterns_3770/image_thumb_1.png" width="244" align="left" border="0" /&gt;&lt;/a&gt;The template test class will contain &lt;em&gt;abstract&lt;/em&gt; methods as unit tests, which any derived class would have to override and perform. That means that if you are always testing the same things against a data layer object (insert returns different IDs, update works etc...) test authors on new data layer classes who derive from this class will never forget to implement at least these basic tests.&lt;/p&gt;  &lt;p&gt;they would also get the facilities for doing assertions and other things from the base class, as depicted in the previous pattern. makes sure you don&amp;#39;t forget tests, saves some time on the infrastructure.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Abstract Driver Test Class&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/DataLayerTestingTestInheritancePatterns_3770/image_6.png"&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 10px 0px 0px;border-left:0px;border-bottom:0px;" height="244" alt="image" src="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/DataLayerTestingTestInheritancePatterns_3770/image_thumb_2.png" width="228" align="left" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This class will have &lt;em&gt;non abstract&lt;/em&gt; test case methods inside it that will always run the same way. these test methods will call (using abstract methods) to the derived class&amp;#39; implementation of type specific actions (insert specific for a category, or update specific to person) .&lt;/p&gt;  &lt;p&gt;That leaves the derived class to only implement very basic things such as insert (and return the ID), update, or delete (by ID). the test methods in the base class will do the rest of the work. derived classes don&amp;#39;t need to implement any tests unless they want to add type specific tests which don&amp;#39;t belong on the base class.&lt;/p&gt;  &lt;p&gt;in the image to the left the green methods are tests that the base class runs, and the yellow ones are type specific helpers that the derived class has to implement.&lt;/p&gt;  &lt;p&gt;this makes writing tests for a new data layer object (as long as its interface is the same) very simple by inheriting a test class and implementing a few methods. all the test permutations in the base class will then run on the data layer object. saves lots of time and forgetting tests.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;What patterns have you come across?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5369330" width="1" height="1" alt="" /&gt;</description></item><item><title>Mocking LINQ Queries, Extension methods and Anonymous Types</title><link>http://agileisrael.org/cs/blogs/royo/archive/2007/11/17/mocking-linq-queries-extension-methods-and-anonymous-types.aspx</link><pubDate>Sat, 17 Nov 2007 21:47:14 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:649</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;One of the things I wanted to show at my Interactive session on unit testing tips and tricks at TechEd this year was how you can &amp;quot;Stub&amp;quot; out results from LINQ Queries, or mock\stub Extension methods in .NET 3.5 (&lt;a href="http://weblogs.asp.net/rosherove/archive/2007/09/16/mocks-and-stubs-the-difference-is-in-the-flow-of-information.aspx"&gt;what&amp;#39;s the difference&lt;/a&gt; between mocks and stubs?)&lt;/p&gt;  &lt;p&gt;The only mocking framework in existence that can truly stub out LINQ queries and extension methods is &lt;a href="http://www.TypeMock.com"&gt;TypeMock.NET&lt;/a&gt;.&amp;amp;#xA0; Another option, if you use LINQ to SQL, is to be able to mock out the DataContext and the IOrderedQueriable interface, &lt;a href="http://aabs.wordpress.com/2007/06/26/using-mock-objects-when-testing-linq-code/"&gt;as outlined here&lt;/a&gt;, to return a custom query result set.&amp;amp;#xA0; I&amp;#39;ll talk about the first option in this post, since it is much easier to read and understand. I may touch on the second one in a later post.&lt;/p&gt;  &lt;p&gt;TypeMock, I&amp;#39;ve &lt;a href="http://weblogs.asp.net/rosherove/archive/2006/09/27/TypeMock-has-a-blog.aspx"&gt;talked about&lt;/a&gt; it &lt;a href="http://weblogs.asp.net/rosherove/archive/2005/03/20/395261.aspx"&gt;before&lt;/a&gt;, is a very powerful mocking framework. much more so than Rhino Mocks or NMock, because it allows isolating static methods, private methods, constructors, and basically anything you can do in IL, because it uses the .NET Profiler APIs to intercept method calls and do whatever it wants with them.&amp;amp;#xA0; In that regard, it is almost &lt;strong&gt;too&lt;/strong&gt; powerful&lt;strong&gt; &lt;/strong&gt;because it rids of of the need to actually design your code for testability, and just test it as is (how you actually write the tests is another matter for another post).&lt;/p&gt;  &lt;p&gt;But I&amp;#39;d rather have the argument whether TypeMock is good or bad, than not have TypeMock on the scene at all, simply because it&amp;#39;s just too damn valuable as is (disclosure: TypeMock is owned by a friend of mine).&lt;/p&gt;  &lt;p&gt;It&amp;#39;s the only real framework that can deal with real untestable legacy code and still allow you to isolate classes enough to test them without changing the code under test itself.&lt;/p&gt;  &lt;p&gt;It&amp;#39;s the only framework that allows to write unit tests for code that is based on .NET 3.5 and uses LINQ Queries and extension methods, allowing to isolate these two types of code and thus be able to separate things from their dependencies.&lt;/p&gt;  &lt;p&gt;Here are some examples I wanted to show (takes fro the examples that come with the TypeMock download:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Example: Stubbing out LINQ Query Results&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First, we&amp;#39;re going to create one fake result to return (array) from the LINQ query, and also introduce the real data source from qhich to query (realCustomerList). note that the customer list has 3 entries, and the fake list has only two entries.&lt;/p&gt;  &lt;pre class="csharpcode"&gt; &lt;span class="rem"&gt;///Two Customer instances used as fake values &lt;/span&gt;&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;&lt;/span&gt; &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Customer fakeCustomer1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer { Id = 0, Name = &lt;span class="str"&gt;&amp;quot;Fake1&amp;quot;&lt;/span&gt;, City = &lt;span class="str"&gt;&amp;quot;SF&amp;quot;&lt;/span&gt; };&lt;/pre&gt;

&lt;pre class="csharpcode"&gt; &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Customer fakeCustomer2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer { Id = 1, Name = &lt;span class="str"&gt;&amp;quot;Fake2&amp;quot;&lt;/span&gt;, City = &lt;span class="str"&gt;&amp;quot;Redmond&amp;quot;&lt;/span&gt; }; &lt;span class="rem"&gt;/// A fake list used as return value in the tests&lt;/span&gt; &lt;span class="kwrd"&gt;private&lt;/span&gt; List&amp;lt;Customer&amp;gt; fakeCustomers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Customer&amp;gt; {fakeCustomer1,fakeCustomer2 }; &lt;span class="rem"&gt;/// A list containing 3 cusotmer use as the &amp;quot;real data&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;private&lt;/span&gt; List&amp;lt;Customer&amp;gt; realCustomerList = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Customer&amp;gt; { &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer{ Id = 1, Name=&lt;span class="str"&gt;&amp;quot;Dave&amp;quot;&lt;/span&gt;, City=&lt;span class="str"&gt;&amp;quot;Sarasota&amp;quot;&lt;/span&gt; }, &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer{ Id = 2, Name=&lt;span class="str"&gt;&amp;quot;John&amp;quot;&lt;/span&gt;, City=&lt;span class="str"&gt;&amp;quot;Tampa&amp;quot;&lt;/span&gt; }, &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer{ Id = 3, Name=&lt;span class="str"&gt;&amp;quot;Abe&amp;quot;&lt;/span&gt;, City=&lt;span class="str"&gt;&amp;quot;Miami&amp;quot;&lt;/span&gt; } };&lt;/pre&gt;

&lt;p&gt;Here&amp;#39;s how the unit test looks:&lt;/p&gt;

&lt;p&gt;&amp;amp;#xA0;&lt;/p&gt;

&lt;p&gt;&amp;amp;#xA0;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt; 1: &lt;/span&gt;[TestMethod]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 2: &lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; MockSimpleQuery()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 3: &lt;/span&gt; {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 4: &lt;/span&gt; &lt;span class="kwrd"&gt;using&lt;/span&gt; (RecordExpectations r = &lt;span class="kwrd"&gt;new&lt;/span&gt; RecordExpectations())&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 5: &lt;/span&gt; {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 6: &lt;/span&gt; var answer = from c &lt;span class="kwrd"&gt;in&lt;/span&gt; realCustomerList select c;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 7: &lt;/span&gt; r.Return(fakeCustomers);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 8: &lt;/span&gt; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 9: &lt;/span&gt;&amp;amp;#xA0;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 10: &lt;/span&gt; var actual = from c &lt;span class="kwrd"&gt;in&lt;/span&gt; realCustomerList select c;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 11: &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 12: &lt;/span&gt; Assert.AreEqual(2, actual.Count&amp;lt;Customer&amp;gt;());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 13: &lt;/span&gt; Assert.IsTrue(actual.Contains(fakeCustomer1));&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 14: &lt;/span&gt; Assert.IsTrue(actual.Contains(fakeCustomer2));&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 15: &lt;/span&gt; }&lt;/pre&gt;
&lt;/div&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

&lt;p&gt;&lt;strong&gt;Line 6 &lt;/strong&gt;is the one that tells the TypeMock record what LINQ query to intercept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 7 &lt;/strong&gt;tells TypeMock what value to return when this query is intercepted. that means the query will never really take place. &lt;/p&gt;

&lt;p&gt;finally, we assert that what we got from the query is the fake list of customers. Pretty darn easy.&lt;/p&gt;

&lt;p&gt;&amp;amp;#xA0;&lt;/p&gt;

&lt;p&gt;But is this a good test? Not really. this is only a demo of what you code intercept with typeMock. a real unit test will actually test a piece of code that runs this query, and, instead of providing it with a list of objects to query, will just provide it with the fake return value to receive&lt;/p&gt;

&lt;p&gt;&amp;amp;#xA0;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Example: Test that an anonymous type is created correctly&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt; 1: &lt;/span&gt; [TestMethod]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 2: &lt;/span&gt; [VerifyMocks]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 3: &lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; MockAnonymousTypeTest()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 4: &lt;/span&gt; {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 5: &lt;/span&gt; &lt;span class="kwrd"&gt;using&lt;/span&gt; (RecordExpectations rec = &lt;span class="kwrd"&gt;new&lt;/span&gt; RecordExpectations())&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 6: &lt;/span&gt; {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 7: &lt;/span&gt; &lt;span class="rem"&gt;//Mock the creation of the Anonymous Type&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 8: &lt;/span&gt; var p1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; { Name = &lt;span class="str"&gt;&amp;quot;A&amp;quot;&lt;/span&gt;, Price = 3 };&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 9: &lt;/span&gt; rec.CheckArguments(); &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 10: &lt;/span&gt;&amp;amp;#xA0;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 11: &lt;/span&gt; &lt;span class="rem"&gt;//fake the value of the Name Property&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 12: &lt;/span&gt; rec.ExpectAndReturn(p1.Name, &lt;span class="str"&gt;&amp;quot;John&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 13: &lt;/span&gt; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 14: &lt;/span&gt; &lt;span class="rem"&gt;//if creation will be done diffrently an exception will be thrown.&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 15: &lt;/span&gt; var target = &lt;span class="kwrd"&gt;new&lt;/span&gt; { Name = &lt;span class="str"&gt;&amp;quot;B&amp;quot;&lt;/span&gt;, Price = 3 };&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 16: &lt;/span&gt; &lt;span class="rem"&gt;//verify that the fake value is returned&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 17: &lt;/span&gt; Assert.AreEqual(&lt;span class="str"&gt;&amp;quot;John&amp;quot;&lt;/span&gt;, target.Name);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 18: &lt;/span&gt; }&lt;/pre&gt;
&lt;/div&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

&lt;p&gt;&amp;amp;#xA0;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 8 &lt;/strong&gt;tells the typemock recorder that this is the anonymous type creation we&amp;#39;d like to intercept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 9 &lt;/strong&gt;tell it to assert internally that the anonymous type is indeed created with the correct &amp;quot;Name&amp;quot; property value and the correct &amp;quot;Price&amp;quot; property value. so if I later initialize the anonymous type with the wrong values, I will get a test exception based on the expected values. Pretty neat.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 12 &lt;/strong&gt;is just an example of how you can also intercept and retun whatever value you&amp;#39;d like from the property of an anonymous type. in this case the name will always return &amp;quot;John&amp;quot; even though it may have been initialized differently.&lt;/p&gt;

&lt;p&gt;&amp;amp;#xA0;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Example: Mocking Extension Methods&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Extension methods are static methods, which means you can&amp;#39;t replace them with a different method instance. there are ways to design yoru code so that the calls to the static methods are testable, but assuming you&amp;#39;re testing code that cannot be changed, or that is hard to redesign, intercepting the method call itself is almost the only choice. &lt;/p&gt;

&lt;p&gt;assume you&amp;#39;ve extended the Point class by adding this extension method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Extend { &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Point Multiply(&lt;span class="kwrd"&gt;this&lt;/span&gt; Point extendedInstance, &lt;span class="kwrd"&gt;int&lt;/span&gt; scalar) { extendedInstance.X *= scalar; extendedInstance.Y *= scalar; &lt;span class="kwrd"&gt;return&lt;/span&gt; extendedInstance; } }&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&amp;amp;#xA0;&lt;/pre&gt;

&lt;p&gt;Now you could write a test like this:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt; 1: &lt;/span&gt; [TestMethod]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 2: &lt;/span&gt; [VerifyMocks]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 3: &lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; MockExtensionMethod()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 4: &lt;/span&gt; {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 5: &lt;/span&gt; &lt;span class="kwrd"&gt;using&lt;/span&gt; (RecordExpectations rec = &lt;span class="kwrd"&gt;new&lt;/span&gt; RecordExpectations())&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 6: &lt;/span&gt; {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 7: &lt;/span&gt; Point mocked = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(7, 9);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 8: &lt;/span&gt; mocked.Multiply(6);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 9: &lt;/span&gt; rec.Return(&lt;span class="kwrd"&gt;new&lt;/span&gt; Point(5, 5));&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 10: &lt;/span&gt; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 11: &lt;/span&gt;&amp;amp;#xA0;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 12: &lt;/span&gt; Point target = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(7, 9);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 13: &lt;/span&gt; Point actual = target.Multiply(6);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 14: &lt;/span&gt;&amp;amp;#xA0;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 15: &lt;/span&gt; &lt;span class="rem"&gt;//Verify the returned vcalues&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 16: &lt;/span&gt; Assert.AreEqual(5, actual.X);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 17: &lt;/span&gt; Assert.AreEqual(5, actual.Y);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt; 18: &lt;/span&gt; }&lt;/pre&gt;
&lt;/div&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

&lt;p&gt;&amp;amp;#xA0;&lt;/p&gt;

&lt;p&gt;in line 7 we&amp;#39;re actually using the extension method as part of our recorder, and telling the record to return a specific value. then we later use that value in our code under test (like 13) and assert that we god the mocked value instead of the real one (the extension method is not executed).&lt;/p&gt;

&lt;p&gt;&amp;amp;#xA0;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about other technologies?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;could you use this to test code that relies on other non testable technologies, like windows workflow, WCF etc? the answer is a big yes. the same principles apply, and most of these frameworks were not built with testability in mind. &lt;/p&gt;

&lt;p&gt;it&amp;#39;s in those cases where people ask &amp;quot;how can I test such code where everything is sealed, private and static?&amp;quot; that I tell them that, currently, TypeMock is the only framework that solves this problem at a satisfactory result.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5261344" width="1" height="1" alt="" /&gt;</description></item><item><title>The various meanings of TDD</title><link>http://agileisrael.org/cs/blogs/royo/archive/2007/10/08/the-various-meanings-of-tdd.aspx</link><pubDate>Tue, 09 Oct 2007 01:49:57 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:548</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;Now that I&amp;#39;m having some real time to write something I thought I&amp;#39;d try to write down my thoughts starting all the way back from &lt;a href="http://weblogs.asp.net/rosherove/archive/2007/09/27/things-i-learned-at-jaoo-2007.aspx"&gt;JAOO and what I learned from it.&lt;/a&gt;&amp;nbsp;Specifically the whole notion of TDD, and what does TDD mean to different people.&lt;/p&gt; &lt;p&gt;&amp;quot;TDD will deteriorate your design&amp;quot;. that was the sentence I heard coming out from the mouth of &lt;a href="http://www.rcn.com/users/jcoplien/"&gt;Jim Coplien&lt;/a&gt;, a speaker at JAOO and a well respected guru in the software industry in many levels. I tried to grasp what he was saying again, so I repeated the sentence in my head. &amp;quot;TDD will deteriorate your design, so don&amp;#39;t do it&amp;quot;. Yes. I heard correctly. &lt;/p&gt; &lt;p&gt;It was lined throughout the discussion Jim was having about Scrum and Architecture and was just one small point he made which he didn&amp;#39;t even take the time to review fully. &amp;quot;Will deteriorate your design&amp;quot; and then nothing. Someone from the crowd of about 400 challenged him and Jim was starting to quote a couple of research papers which I don&amp;#39;t have a link to so I have not read them.&amp;nbsp; Jim was kind enough to leave a comment on my &lt;a href="http://weblogs.asp.net/rosherove/archive/2007/09/27/things-i-learned-at-jaoo-2007.aspx"&gt;previous post&lt;/a&gt; about JAOO but that did not include any links to those research papers. Could anyone leave a comment here with a link to them?&lt;/p&gt; &lt;p&gt;When Jim walked out of the hall I started talking to him for what ended up to be about 40 minutes of intense rhetorical debate of what does TDD really mean and then what benefit if at all does it have. I&amp;#39;ll weave points from that in but first I need to say that I realized something: TDD is not a clear enough concept so people see it in different light depending on who you ask. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;u&gt;What are the possible meanings of TDD?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;u&gt;&lt;strong&gt;1) Test Driven &lt;/strong&gt;Development&lt;/u&gt;: the idea of writing your code in a &lt;strong&gt;test first &lt;/strong&gt;manner. You may already have an existing design in place.&lt;/li&gt; &lt;li&gt;&lt;u&gt;&lt;strong&gt;2) Test Oriented &lt;/strong&gt;Development&lt;/u&gt;: Having unit tests of integration tests in your code and write them out either before or after our write the code. Your code has lots of tests. You recognize the value of tests but you don&amp;#39;t necessarily write them before you write the code. Design probably exists before you write the code&lt;/li&gt; &lt;li&gt;&lt;u&gt;&lt;strong&gt;3) &lt;/strong&gt;Test Driven &lt;/u&gt;&lt;strong&gt;&lt;u&gt;Design&lt;/u&gt;(&lt;/strong&gt;the eXtreme Programming way):&amp;nbsp; The idea of using a test-first approach as a fully fledged design technique, where tests are a bonus but the idea is to drive full design from &lt;strong&gt;little to no design &lt;/strong&gt;whatsoever. You design as you go. &lt;/li&gt; &lt;li&gt;&lt;u&gt;&lt;strong&gt;4)&lt;/strong&gt; Test Driven &lt;strong&gt;Development and Design&lt;/strong&gt;&lt;/u&gt;: using the test-first technique to drive new code and changes, while also allowing it to change and evolve your design as an added bonus. You may already have some design in place before starting to code, but it could very well change because the tests point out various smells.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;It&amp;#39;s important to realize the distinction between these four perceived notions of what TDD could mean because when Jim said &amp;quot;don&amp;#39;t do TDD&amp;quot; he meant one of these. Unfortunately he didn&amp;#39;t really explain which one he meant (maybe he hadn&amp;#39;t realized they exist) so the message he gave out was essentially &amp;quot;Whatever you think TDD is, don&amp;#39;t do it&amp;quot;. &lt;/p&gt; &lt;p&gt;Which of these notions corresponds to what you think TDD is? which do you think I would choose? &lt;/p&gt; &lt;p&gt;From my conversations with Jim it would seem that he think about TDD as option number 3 - Test Driven Design.&amp;nbsp;He was saying that if you do it the XP was where you&amp;nbsp;let the tests drive all of your architecture you&amp;#39;re bound to get into trouble so leave it alone. I&amp;#39;m leaving the argument of whether pure test driven design works or not&amp;nbsp; for later. Right now I want to make sure we&amp;#39;re all on the same line.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;If you&amp;#39;re going to&amp;nbsp;say TDD doesn&amp;#39;t work - explain what you mean by TDD &lt;/strong&gt;or be prepared to be bombarded with questions which you may feel have no connection with what you&amp;#39;re talking about. &amp;nbsp;&lt;/p&gt; &lt;p&gt;My notion of TDD is #4 - Test Driven development which can also help evolve and better your design. I always start with some sort of design, even a basic one. Sometimes so basic it&amp;#39;s all in my head or on a whiteboard, but there&amp;#39;s some skeleton there I use to guide me. I also let it change if my tests point out that there might be some testability problem here and there. I&amp;#39;m not saying pure Test Driven Design does not work. I&amp;#39;m sure it works for people, and I&amp;#39;m sure if failed others. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;The Alpha Architect and Test Driven Design&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;One interesting thing that came up in that conversation (which many people assembled and chipped in with their on thoughts in) was the idea of the &amp;quot;Alpha Architect&amp;quot;. There seemed to be agreement that the people who can do pure test driven design (like &lt;a href="http://www.xprogramming.com/Blog/Page.aspx?display=HotNeedleOfInquiry"&gt;Ron Jefferies&lt;/a&gt;) probably already hold all the design they need in their head, so they don&amp;#39;t get why other people can&amp;#39;t do the same thing. These &amp;quot;Alpha Architects&amp;quot; are pretty unique and perhaps live in some sort of bubble where things like TDDesign feel much easier and simpler.&amp;nbsp; I&amp;#39;m not sure that&amp;#39;s the case but I can understand the idea and willing to agree that it&amp;#39;s possible. &lt;/p&gt; &lt;p&gt;So, is Test Driven Design only good for teams with Alpha Architects in them? not sure, but it&amp;#39;s a question worth asking.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;So, is TDD Bad?&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Are the rest of the notions of TDD bad? In our talks Jim agreed that unit tests or integration tests might not be a bad idea after all as long as they don&amp;#39;t kill your design. When he said &amp;quot;but I was saying TDD kills design I said it because that&amp;#39;s what people think TDD is!&amp;quot;. The only reply to that is that by telling them that you are only reinforcing their belief that they know what TDD is, and not even questioning their assumption (or yours) that it could be a good thing.&lt;/p&gt; &lt;p&gt;I think that TDDAD(Development and Design) is a good thing. I&amp;#39;d love for my assumptions to be challenged. I&amp;#39;m still waiting to get the links to the two research articles mentioned. &lt;/p&gt; &lt;p&gt;your comments are appreciated.&lt;/p&gt; &lt;p&gt;&lt;em&gt;possible topics for the future posts:&lt;/em&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;em&gt;Things I realized about XUnit from talking to Brad and Jim&lt;/em&gt;&lt;/li&gt; &lt;li&gt;&lt;em&gt;BDD vagueness and tooling&lt;/em&gt;&lt;/li&gt; &lt;li&gt;&lt;em&gt;Ruby coolness&lt;/em&gt;&lt;/li&gt; &lt;li&gt;&lt;em&gt;The ALT.NET Conference roundup&lt;/em&gt;&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4504018" width="1" height="1" alt="" /&gt;</description></item><item><title>The Art Of Unit Testing - Available to Purchase Online Now</title><link>http://agileisrael.org/cs/blogs/royo/archive/2007/09/26/the-art-of-unit-testing-available-to-purchase-online-now.aspx</link><pubDate>Wed, 26 Sep 2007 15:49:42 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:452</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;a href="http://www.manning.com/affiliate/idevaffiliate.php?id=462_91"&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 5px 0px 0px;border-left:0px;border-bottom:0px;" height="202" alt="image" src="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/TheArtOfUnitTestingAvailabletoPurchaseOn_FAB2/image_1.png" width="240" align="left" border="0" /&gt;&lt;/a&gt; I&amp;#39;m happy to announce that you can now purchase my book &lt;a href="http://www.manning.com/affiliate/idevaffiliate.php?id=462_91" target="_blank"&gt;The Art Of Unit Testing&lt;/a&gt; through the Manning Early Access Program. Currently, the first five chapters are available, but when you purchase the MEAP version you get the new chapters as they come in, before anyone else. Also, you can help make the book better and more accurate as a MEAP reader since the author forum can be used to discuss book topics you find interesting and I will be reading it at least twice a week (hopefully daily).&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;strong&gt;What&amp;#39;s with the ugly cover art?&lt;/strong&gt; I&amp;#39;m working on it. We had to have something to start putting this online. The final print will have something which I can actually stand behind.&lt;/li&gt; &lt;li&gt;What are the first five chapters about?&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The way I see it, the book is divided into several parts. the first part (chapters 1-5) are about the basic tools you need to know to start writing unit tests in the real world. Everything from working with NUnit and the basic attributes of unit tests, and up to using fake, stub and Mock objects. There is also quite a full reference for using the Rhino.Mocks framework. The idea is that you can take someone with no unit testing experience whatsoever in .NET, and have them read those five chapters. then they have the basic knowledge to actually know what you&amp;#39;re talking about when you say &amp;quot;Mocks&amp;quot; or &amp;quot;integration testing&amp;quot;. They may even be able to write some real world tests using these chapters, even interaction tests. &lt;/p&gt; &lt;p&gt;&lt;b&gt;Part I Getting started&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&amp;nbsp;1.&lt;/b&gt; &lt;a href="http://www.manning-source.com/books/osherove/osherove_meapch1.pdf"&gt;The basics of unit testing&lt;/a&gt; - FREE&lt;br /&gt;&lt;b&gt;&amp;nbsp;2. &lt;/b&gt;The first unit test - AVAILABLE&lt;br /&gt;&lt;b&gt;Part II Core techniques&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&amp;nbsp;3. &lt;/b&gt;Using Stubs to break dependencies - AVAILABLE&lt;br /&gt;&lt;b&gt;&amp;nbsp;4. &lt;/b&gt;Interaction testing Using Mock Objects - AVAILABLE&lt;br /&gt;&lt;b&gt;&amp;nbsp;5. &lt;/b&gt;Mock Object frameworks - AVAILABLE&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4179285" width="1" height="1" alt="" /&gt;</description></item><item><title>Code-Reuse in unit tests is more important than you think</title><link>http://agileisrael.org/cs/blogs/royo/archive/2007/09/22/code-reuse-in-unit-tests-is-more-important-than-you-think.aspx</link><pubDate>Sat, 22 Sep 2007 10:51:06 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:444</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;Scott Bellware mentions how he really likes the new ideas in XUnit.NET, especially the lack of [Setup] and [TearDown] methods. (Brad Wilson wrote in the comments to my post about XUnit.NET that you can mimic this behavior by using the constructor of the test class and the IDisposable.Dispose() method of the class -just implement IDisposable) .&lt;/p&gt; &lt;p&gt;He especially says this:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&amp;quot;I think that Jim is on the right track, but I&amp;#39;m the kind of guy that feels that a test class&amp;#39;s greatest responsibility is to document behavior in the clearest possible way, even if that means sacrificing &lt;strong&gt;design qualities best reserved for functional code - like reuse&lt;/strong&gt;.&amp;quot;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;(emphasis mine)&lt;/p&gt; &lt;p&gt;That&amp;#39;s silly. Scott tries to write &amp;quot;pure&amp;quot; TDD tests by saying that tests are 100% different than functional code, but they are not. Proof -- Tests that don&amp;#39;t have code re-use in the form of setup and teardown or various specialized factory methods will be harder to maintain because every time you change some semantic aspect of your production code (constructors, initialization phases etc..) many of your tests will break and you&amp;#39;ll have to fix them all one by one because you don&amp;#39;t have re-use for object creation in your tests. Pretty soon you&amp;#39;ll just stop maintaining your tests and that means you&amp;#39;ll stop writing new ones. Ta-da! mission accomplished. &lt;/p&gt; &lt;p&gt;If we don&amp;#39;t all realize that in the &lt;strong&gt;real world&lt;/strong&gt; tests have to be as maintainable (or even more so) as production code, we won&amp;#39;t come out of the middle ages of unit testing. &lt;/p&gt; &lt;p&gt;he then goes on to say some rules he has about Setup methods. one of which is:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&amp;quot;Don&amp;#39;t put anything in a setup method that doesn&amp;#39;t pertain to every test method in the test class&amp;quot;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;I completely agree with this. You NEED to have re-use but you also need to make sure you don&amp;#39;t abuse these abilities. &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4041279" width="1" height="1" alt="" /&gt;</description></item><item><title>TDD Pattern: Composite Logic Development and Testing</title><link>http://agileisrael.org/cs/blogs/royo/archive/2007/09/18/tdd-pattern-composite-logic-development-and-testing.aspx</link><pubDate>Tue, 18 Sep 2007 12:32:26 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:411</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;Here&amp;#39;s a video I made that captures my current thoughts on writing complex business rules which have lots of validation logic.&lt;/p&gt; &lt;p&gt;Here&amp;#39;s the scenario I show in the video:&lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/TDDPatternCompositeLogicDevelopmentandTe_CC70/image_2.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="320" alt="image" src="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/TDDPatternCompositeLogicDevelopmentandTe_CC70/image_thumb_2.png" width="457" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;A Validate method on an object actually uses lots of smaller methods to return a result. The idea is to actually write separate tests for the underlying helper methods (which are actually individual rule checks) and a separate test for the overall logic driver (the Validate method. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Different from traditional TDD technique&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;This is different from the traditional way I would do TDD in that in the traditional sense, in TDD you probably would not have these helper methods but would actually write all that logic inside the validate method, and only later you would refactor the code into separate smaller methods. &lt;/p&gt; &lt;ul&gt; &lt;li&gt;TDD: Start with a simple method and refactor it into smaller methods if and when you need to while making the tests pass.&lt;/li&gt; &lt;li&gt;This method (Composite Logic TDD - CLTDD???): drive the creation of the separate rules as part of the TDD effort (part of the initial design).&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;But why? Because every time I tried to accomplish the same thing with the traditional way I came up with problems thinking about my next tests. every time I would add another rules to the implementation it would break my existing tests because they had to account for my new rule. &lt;/p&gt; &lt;p&gt;In the new method I gain full control over what the rules actually return so that I can tell the validate method - &amp;quot;Assume that all the rules pass, except this one&amp;quot;. then I can go on and test things separately. here&amp;#39;s what I mean:&lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/TDDPatternCompositeLogicDevelopmentandTe_CC70/image_4.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="353" alt="image" src="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/TDDPatternCompositeLogicDevelopmentandTe_CC70/image_thumb_4.png" width="560" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;So the video details how you would write all this in a test driven manner. I&amp;#39;m personally not entirely happy with it and i think the technique is still rough around the edges (takes time to learn, not very intuitive etc..) but it&amp;#39;s currently the only way I could come up with to test the logical driven separately from the individual rules it uses. &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Separation of concerns&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The thing I like about the end result is that the individual rules methods (HasDIgit() etc..) can be coded and tested independently from the logic that uses them. much like separating a call to a web service from the actual web service implementation and testing them separately. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Your input&amp;nbsp;is welcome. I&amp;#39;m looking for:&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Giving a proper name for this technique\pattern (the XUNit test patterns book calls the pattern &amp;quot;Test Specific Subclass&amp;quot; with the variation of &amp;quot;Behavior changing subclass&amp;quot; &amp;nbsp; but what I&amp;#39;m doing is a pattern for TDD that tests a composite logic piece of code. what should we call this technique?&lt;/li&gt; &lt;li&gt;Tell me where I&amp;#39;m missing something in the technique that could lead to potential problems&lt;/li&gt; &lt;li&gt;tell me if you think you have a better way of accomplishing this (in TDD or not)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;Here&amp;#39;s the video, 44 minutes.(the demo of the validate method starts at minute 21)&lt;/p&gt; &lt;p&gt; &lt;object id="MediaPlayer" height="270" width="350"&gt; &lt;embed type="application/x-mplayer2" src="http://files.osherove.com/downloads/video/TDDConstraintTesting.wmv" width="350" height="270"&gt;&lt;/embed&gt; &lt;/object&gt;&lt;/p&gt; &lt;p&gt;in the video(you can also &lt;a href="http://files.osherove.com/downloads/video/TDDConstraintTesting.wmv" target="_blank"&gt;download it 57 MB&lt;/a&gt;):&lt;/p&gt; &lt;ul&gt; &lt;li&gt;understanding the problem&lt;/li&gt; &lt;li&gt;the different kinds of tests for this problem&lt;/li&gt; &lt;li&gt;a simple demo of the extract and override pattern&lt;/li&gt; &lt;li&gt;the full demo of using the Validate method&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;a href="http://files.osherove.com/downloads/Video/TDDCompositeRules.zip" target="_blank"&gt;here are the slides&lt;/a&gt; for this video.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3941152" width="1" height="1" alt="" /&gt;</description></item><item><title>Mocks and Stubs - The difference is in the flow of information</title><link>http://agileisrael.org/cs/blogs/royo/archive/2007/09/16/mocks-and-stubs-the-difference-is-in-the-flow-of-information.aspx</link><pubDate>Sun, 16 Sep 2007 21:26:55 GMT</pubDate><guid isPermaLink="false">23a435e0-515b-467d-ba86-b6d13375d82b:384</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;In class today when explaining the difference between stubs and mocks, I realized a simple explanation to this:&lt;/p&gt; &lt;p&gt;Stubs and mocks may seem the same but the flow of information from each is very different.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Stubs provide input for the application under test so that the test can be performed on something else.&lt;/li&gt; &lt;li&gt;Mocks provide input to the test to decide on pass\fail. the opposite direction.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The following diagram shows the flow of critical information:&lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/MocksandStubsThedifferenceisintheflowofi_149B8/image_1.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="367" alt="image" src="http://weblogs.asp.net/blogs/rosherove/WindowsLiveWriter/MocksandStubsThedifferenceisintheflowofi_149B8/image_thumb_1.png" width="387" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;The red arrows signify the &amp;quot;critical&amp;quot; flow of information that is used to decide if the test had passed. A stub can never fail a test, so there is no flow of information back to the test. the mock records things and then tells our test if it&amp;#39;s expectations have been met according to its recording. &lt;/p&gt; &lt;p&gt;A stub is application facing, and a mock is test facing. &lt;a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank"&gt;Here&amp;#39;s a more in depth overview of the two&lt;/a&gt;, but I haven&amp;#39;t seen this explained as I just did anywhere. It&amp;#39;s important to know and distinguish the two since many frameworks for mocking use these terms for different kinds of objects.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3898819" width="1" height="1" alt="" /&gt;</description></item></channel></rss>