<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://bloggingabout.net/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>Vagif Abilov's blog on .NET : TypeMock, Isolator</title><link>http://bloggingabout.net/blogs/vagif/archive/tags/TypeMock/Isolator/default.aspx</link><description>Tags: TypeMock, Isolator</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>.NET mocking framework comparison</title><link>http://bloggingabout.net/blogs/vagif/archive/2009/10/14/net-mocking-framework-comparison.aspx</link><pubDate>Wed, 14 Oct 2009 14:03:21 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:482325</guid><dc:creator>Vagif Abilov</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/rsscomments.aspx?PostID=482325</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/commentapi.aspx?PostID=482325</wfw:comment><comments>http://bloggingabout.net/blogs/vagif/archive/2009/10/14/net-mocking-framework-comparison.aspx#comments</comments><description>&lt;p&gt;A couple of good sources for those who need to decide:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.phpvs.net/2009/04/25/net-mocking-frameworks-capability-comparison/" target="_blank"&gt;Capability comparison&lt;/a&gt; made by &lt;span class="Apple-style-span" style="word-spacing:0px;font:medium &amp;#39;Times New Roman&amp;#39;;text-transform:none;text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"&gt;&lt;span class="Apple-style-span" style="font-size:12px;line-height:18px;font-family:verdana, tahoma, arial, serif;"&gt;Morgan Soley. Half year old but with a feature comparison table.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span class="Apple-style-span" style="word-spacing:0px;font:medium &amp;#39;Times New Roman&amp;#39;;text-transform:none;text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"&gt;&lt;span class="Apple-style-span" style="font-size:12px;line-height:18px;font-family:verdana, tahoma, arial, serif;"&gt;&lt;a href="http://codevanced.net/post/Mocking-frameworks-comparison.aspx" target="_blank"&gt;Recently updated comparison&lt;/a&gt; by Andrew Kazyrevich.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span class="Apple-style-span" style="word-spacing:0px;font:medium &amp;#39;Times New Roman&amp;#39;;text-transform:none;text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"&gt;&lt;span class="Apple-style-span" style="font-size:12px;line-height:18px;font-family:verdana, tahoma, arial, serif;"&gt;TypeMock Isolator is a clear winner if it fits in your budget. Otherwise Rhino Mocks or Moq seem to be the most mature frameworks. According to a &lt;a href="http://weblogs.asp.net/rosherove/archive/2009/09/30/poll-which-mocking-isolation-framework-do-you-use.aspx" target="_blank"&gt;recent Roy Osherove’s pole&lt;/a&gt; they are in the lead with Rhino Mocks having 47% and Moq on second place with 34%. If I had to choose a free mocking framework today, I’d probably go with Moq because of Rhino Mocks’ confusing “very large array of syntaxes”. Or wait for Rhino Mocks 4.0 that will be radically simplified and, like Isolator, will go for AAA abd single “fake” concept.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span class="Apple-style-span" style="word-spacing:0px;font:medium &amp;#39;Times New Roman&amp;#39;;text-transform:none;text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"&gt;&lt;span class="Apple-style-span" style="font-size:12px;line-height:18px;font-family:verdana, tahoma, arial, serif;"&gt;But for mocking without severe limitations there is no alternative to Isolator. Still in the lead.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=482325" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/TypeMock/default.aspx">TypeMock</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/unit+testing/default.aspx">unit testing</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/Isolator/default.aspx">Isolator</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/mocking/default.aspx">mocking</category></item><item><title>ASP.NET Bundle from TypeMock</title><link>http://bloggingabout.net/blogs/vagif/archive/2009/05/18/asp-net-bundle-from-typemock.aspx</link><pubDate>Mon, 18 May 2009 16:28:50 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:481660</guid><dc:creator>Vagif Abilov</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/rsscomments.aspx?PostID=481660</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/commentapi.aspx?PostID=481660</wfw:comment><comments>http://bloggingabout.net/blogs/vagif/archive/2009/05/18/asp-net-bundle-from-typemock.aspx#comments</comments><description>&lt;p&gt;TypeMock is has announced ASP.NET Bundle (consisting of Isolator and Ivonna). Being TypeMock user and customer, I am distributing their announcement. The text below is not mine – it’s written by TypeMock and explains how to get a free ASP.NET Bundle license (if you hurry up, like me :-))&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.typemock.com/"&gt;Unit Testing&lt;/a&gt; ASP.NET? &lt;a href="http://www.typemock.com/ASP.NET_unit_testing_page.php"&gt;ASP.NET unit testing&lt;/a&gt; has never been this easy.    &lt;br /&gt;    &lt;br /&gt;Typemock is launching a new product for ASP.NET developers – the &lt;strong&gt;ASP.NET Bundle&lt;/strong&gt; - and for the launch will be giving out &lt;span style="color:#006600;"&gt;&lt;strong&gt;FREE licenses&lt;/strong&gt;&lt;/span&gt; to bloggers and their readers.    &lt;br /&gt;    &lt;br /&gt;The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both &lt;a href="http://www.typemock.com/"&gt;Typemock Isolator&lt;/a&gt;, a &lt;a href="http://www.typemock.com/"&gt;unit test&lt;/a&gt; tool and &lt;a href="http://sm-art.biz/Ivonna.aspx"&gt;Ivonna&lt;/a&gt;, the Isolator add-on for &lt;a href="http://sm-art.biz/Ivonna.aspx"&gt;ASP.NET unit testing&lt;/a&gt;, for a bargain price.    &lt;br /&gt;    &lt;br /&gt;Typemock Isolator is a leading &lt;a href="http://www.typemock.com/"&gt;.NET unit testing&lt;/a&gt; tool (C# and VB.NET) for many ‘hard to test’ technologies such as &lt;a href="http://typemock.com/sharepointpage.php"&gt;SharePoint&lt;/a&gt;, &lt;a href="http://www.typemock.com/ASP.NET_unit_testing_page.php"&gt;ASP.NET&lt;/a&gt;, &lt;a href="http://www.typemock.com/ASP.NET_unit_testing_page.php"&gt;MVC&lt;/a&gt;, &lt;a href="http://www.typemock.com/wcfpage.php"&gt;WCF&lt;/a&gt;, WPF, &lt;a href="http://www.typemock.com/Silverlight_unit_testing_page.php"&gt;Silverlight&lt;/a&gt; and more. Note that for &lt;a href="http://www.typemock.com/Silverlight_unit_testing_page.php"&gt;unit testing Silverlight&lt;/a&gt; there is an open source Isolator add-on called &lt;a href="http://www.typemock.com/Silverlight_unit_testing_page.php"&gt;SilverUnit&lt;/a&gt;.    &lt;br /&gt;    &lt;br /&gt;The first 60 bloggers who will blog this text in their blog and &lt;a href="http://blog.typemock.com/2009/05/get-free-typemock-licenses-aspnet.html"&gt;tell us about it&lt;/a&gt;, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET &lt;strong&gt;dedicated&lt;/strong&gt; blog, you&amp;#39;ll get a license automatically (even if more than 60 submit) during the first week of this announcement.    &lt;br /&gt;    &lt;br /&gt;Also 8 bloggers will get an &lt;strong&gt;additional 2 licenses&lt;/strong&gt; (each) to give away to their readers / friends.    &lt;br /&gt;    &lt;br /&gt;Go ahead, click the following link for &lt;a href="http://blog.typemock.com/2009/05/get-free-typemock-licenses-aspnet.html"&gt;more information &lt;/a&gt;on how to get your free license.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=481660" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/TypeMock/default.aspx">TypeMock</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/Isolator/default.aspx">Isolator</category></item><item><title>TypeMock Isolator and matching faked method’s arguments – part 3</title><link>http://bloggingabout.net/blogs/vagif/archive/2009/03/14/typemock-isolator-and-matching-faked-method-s-arguments-part-3.aspx</link><pubDate>Sat, 14 Mar 2009 22:18:26 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:481346</guid><dc:creator>Vagif Abilov</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/rsscomments.aspx?PostID=481346</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/commentapi.aspx?PostID=481346</wfw:comment><comments>http://bloggingabout.net/blogs/vagif/archive/2009/03/14/typemock-isolator-and-matching-faked-method-s-arguments-part-3.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://bloggingabout.net/blogs/vagif/archive/2009/03/12/how-i-would-match-faked-method-s-arguments-if-i-was-typemock-isolator.aspx"&gt;Part 1&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://bloggingabout.net/blogs/vagif/archive/2009/03/14/typemock-isolator-and-matching-faked-method-s-arguments-part-2.aspx"&gt;Part 2&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I promised to address matching reference and output arguments using lambda-based syntax that I proposed for TypeMock Isolator. Before getting there I would like to retract my original comment that faked method’s arguments should be matched by default. I still think that, like strong typing, strong matching is a best practice to follow. However I should not forget the main goal of my proposal: increase code simplicity and compactnes. My first examples used very simple method calls consisting of one-two arguments, so an extra effort of typing “(int x, int y)” went unnoticed. Using real API methods immediately showed how inconvenient it can become to faked methods with large number of arguments if developer is forced to list argument types even when no argument match is required. Here how it might look:&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;Isolate.WhenCalled((string a, Evidence b, byte[] c, AssemblyHashAlgorithm d) =&amp;gt; 
        Assembly.LoadFrom(a, b, c, d)).WillReturn(null);&lt;/pre&gt;

&lt;p&gt;Note where it gets really terrible: you have to type the whole argument list of LoadFrom: “string a, Evidence b, byte[] c, AssemblyHashAlgorithm d”. All this without help from Intellisense! The argument list comes &lt;em&gt;before&lt;/em&gt; the method name.&lt;/p&gt;

&lt;p&gt;Now compare it with current use of TypeMock API:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalled(() =&amp;gt; Assembly.LoadFrom(null, null, null, 0)).WillReturn(null);&lt;/pre&gt;

&lt;p&gt;What would you choose? With all respect to strong matching the choice is clear.&lt;/p&gt;

&lt;p&gt;Eli suggested defining a different method for faking a method a method when arguments matter, and now I came to the same conclusion. So I will leave WhenCalled alone and use a different method name, let it be called WhenCalledArgs (I am not sure about using word “Exact” in such method name, because this method will support partial match, and in case you only match one argument out of five, the word “exact” can be confusing).&lt;/p&gt;

&lt;p&gt;Now back to agenda: matching reference and output arguments. In my previous post for each size of argument list I defined two overloads (I will be using new name WhenCalledArgs):&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public static IPublicNonVoidMethodHandler WhenCalledArgs(Func func);
public static IPublicNonVoidMethodHandler WhenCalledArgs(Func func, Func predicate);&lt;/pre&gt;

&lt;p&gt;The definitions above cover the case of a method with single argument. The first overload corresponds to a call with no argument match and no checker. The second overload can be used to define a custom checker for an argument. What is missing here is support for reference and output arguments, when the passed value is assigned to an argument marked as “ref” or “out” on method return. Fortunately, this is easy to achive using the same notation. It just requires new oveloads.&lt;/p&gt;

&lt;p&gt;To assign reference and output value we need to extend WhenCalledArgs with a new parameter: an&amp;#160; Action&amp;lt;T&amp;gt; delegate:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public static IPublicNonVoidMethodHandler WhenCalledArgs(Func func, Action output);&lt;/pre&gt;

&lt;p&gt;And to enable specifying both custom checker and output delegate, we will have an overload that combines them all:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public static IPublicNonVoidMethodHandler WhenCalledArgs(Func func, Func predicate, Action output);&lt;/pre&gt;

&lt;p&gt;Similar overloads we will need to provide for void methods (in such case WhenCalledArgs will be returning IVoidActionHandler instead of IPublicNonVoidMethodHandler). Now let’s look at the test code. For the sake of simplicity we assume to be dealing with a static class called MyClass, and the method that we are going to fake has the following signature:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public static void LookupCustomer(int customerId, out string firstName, out string lastName);&lt;/pre&gt;

&lt;p&gt;First we will fake its call but assign on return the values “John” and Smith” to the firstName and lastName respectively. Here how the code will look:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalledArgs((int x, string y, string z) =&amp;gt; 
        MyClass.LookupCustomer(x, out y, out z), (x, y, z) =&amp;gt; { y = &amp;quot;John&amp;quot;; z = &amp;quot;Smith&amp;quot;; }).IgnoreCall();&lt;/pre&gt;

&lt;p&gt;Note that the customerId argument will not be matched. If we want to match it we slightly change the code:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalledArgs((string x, string y) =&amp;gt; 
        MyClass.LookupCustomer(123, out x, out y), (x, y) =&amp;gt; { x = &amp;quot;John&amp;quot;; y = &amp;quot;Smith&amp;quot;; }).IgnoreCall();&lt;/pre&gt;

&lt;p&gt;What happens now is that the method LookupCustomer will only be faked for customerId equals 123, and upon return firstName and lastName arguments will be assigned values “John” and “Smith”.&lt;/p&gt;

&lt;p&gt;Using “ref” instead of “out” does not really change much. It just gives an opportunity to assign (and match) a value to an argument and in addition specify another one on return.&lt;/p&gt;

&lt;p&gt;And of course we can combine assignment of ret/out values with custom argument checker. Here’s an example:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalledArgs((int x, string y, string z) =&amp;gt; 
        MyClass.LookupCustomer(x, out y, out z), (x, y, z) =&amp;gt; x &amp;gt; 100, 
        (x, y, z) =&amp;gt; { y = &amp;quot;John&amp;quot;; z = &amp;quot;Smith&amp;quot;; }).IgnoreCall();&lt;/pre&gt;

&lt;p&gt;This is quite compact notation, however it does various things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It fakes calls to MyClass.LookupCustomer without argument match &lt;/li&gt;

  &lt;li&gt;Even though arguments are not matched, only calls with customerId greater than 100 will be faked, as speficied in custom checker predicate &lt;/li&gt;

  &lt;li&gt;On return output arguments firstName and lastName will be assigned values “John” and “Smith” respectively. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;In these blogs posts I tried to demonstrate how we can take advantage of C# 3.0 language features and provide compact notation for TypeMock Isolator AAA syntax. I am pretty sure there is someting uncovered, and I haven’t even thought about how this approach can be applied to VB (I am afraid it can’t, at least not until Visual Basic gets action-based lambda expression support). However I believe this notation can be successfully applied to Isolator AAA syntax, as it is based on the same principles and can further improve compactness and readability of the code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=481346" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/TypeMock/default.aspx">TypeMock</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/lambda/default.aspx">lambda</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/unit+testing/default.aspx">unit testing</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/Isolator/default.aspx">Isolator</category></item><item><title>TypeMock Isolator and matching faked method’s arguments – part 2</title><link>http://bloggingabout.net/blogs/vagif/archive/2009/03/14/typemock-isolator-and-matching-faked-method-s-arguments-part-2.aspx</link><pubDate>Sat, 14 Mar 2009 10:49:25 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:481340</guid><dc:creator>Vagif Abilov</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/rsscomments.aspx?PostID=481340</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/commentapi.aspx?PostID=481340</wfw:comment><comments>http://bloggingabout.net/blogs/vagif/archive/2009/03/14/typemock-isolator-and-matching-faked-method-s-arguments-part-2.aspx#comments</comments><description>&lt;p&gt;I was glad to see that &lt;a href="http://bloggingabout.net/blogs/vagif/archive/2009/03/12/how-i-would-match-faked-method-s-arguments-if-i-was-typemock-isolator.aspx"&gt;my thoughts&lt;/a&gt; about figuring out the best syntax to control the match of faked method’s arguments haven’t been unnoticed by TypeMockians. &lt;a href="http://www.elilopian.com/"&gt;Eli Lopian&lt;/a&gt; raised a few questions, and addressing them seems to be crucial for materialization of the whole idea.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1. Backward compatibility with existing tests.&lt;/strong&gt; If WhenCalled(() =&amp;gt; some_method(a, b, c)) will be interpreted as a call with exact argument match, existing tests will fail. Parameterless delegates in such calls will have to be replaced with parameterized ones, e.g. WhenCalled((x, y, z) =&amp;gt; some_method(x, y, z)).&lt;/p&gt;  &lt;p&gt;I was quick enough to respond that I would be willing to update all tests just to get the syntax right, but I admit such response is childish. You can’t force your customers to use many hours on fixing their tests just because you discovered great syntax. Of course you need to provide a compatibility mode. There are different ways to achieve that. The simplest is to use a global flag:&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;Isolate.DefaultArgumentMatchBehavior = ArgumentBehavior.ExactMatch;&lt;/pre&gt;

&lt;p&gt;Exact match can be used as default for backward compatibility, at least during transition period. Or argument match mode can be implemented as an attribute that can later be deprecated. I don’t think this looks very elegant (global flags can’t be elegant), this is more like a temporary solution to let tests survive until the next major TypeMock release.&lt;/p&gt;

&lt;p&gt;If this sounds too messy, then alternative approach might be to place functionality based on lambda syntax in a new namespace. Perhaps this is cleaner. I can compare it with approach taken by LINQ developers that divided LINQ classes between two namespaces and placed Expression and its derivations in System.Linq.Expressions. Later in this post I will show new extensions that solve custom checkers, and the more such extensions come, the more it is justified to define a dedicated namespace for lambda-based argument match control. What can work best, I don’t really know. Anyway, I believe resolving backward compatibility is more like configuration issue that should not compromise syntax selection.&lt;/p&gt;

&lt;p&gt;What I find however important is to let a developer set up the tests so the arguments would be matched by default. Already now indexers are matched, this change broke many of our tests (I like defining custom indexers), but I think this was a right thing. When mock framework encounters ar[1], I believe it should respect the index value unless tests are specifically configured to ignore it. The same behavior I expect from faked methods: mock framework should not ignore that a method DoSomething(a, b, c) is expected to be faked with arguments (a, b, c). I do agree that this may not be turned on by default for backward compatibility purpose, but I would like to have a way to turn it on – at least to unifier indexers’ and methods’ behaviour.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Support for custom checkers.&lt;/strong&gt; This is perhaps the most important requirement. If we find binary approach to argument matchings (match/nomatch) not sufficient because in some cases we expect partial argument match, we should also expect support for matching with custom checkers, for example when a string value begins with certain pattern, numerical value is within a certain range etc. I agree that without providing an easy way to define custom checkers the idea that I described in my previous post will stay a purely academical excersise. So I started playing with example code.&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;MyIsolate.WhenCalled((int x, int y) =&amp;gt; MyClass.GetSum(x, y)).WillReturn(6);&lt;/pre&gt;

&lt;p&gt;So how we can extend this code to support custom argument checkers, for example to fake a method only if “x” is greater than 0? Something like this:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;MyIsolate.WhenCalled((int x, int y) =&amp;gt; MyClass.GetSum(x, y) where x &amp;gt; 0).WillReturn(6);&lt;/pre&gt;

&lt;p&gt;Of course this code will never compile, but I admit I imported System.Linq namespace and spent some time shuffling “where” keyword around. Not to resolve it using LINQ – just to get some inspiration. Making mock framework depedent on LINQ would be really mad (and would never work), not to say that both “where” clause is just a syntactic sugar around Where method that requires support for IEnumerable. And what IEnumerable can have with argument matching?&lt;/p&gt;

&lt;p&gt;However turning inspecting implementation of “where” paid of, because the its core part is a predicate delegate Func&amp;lt;T, bool&amp;gt;. This is what we need to extend our WhenCalled with support for custom checkers.&lt;/p&gt;

&lt;p&gt;Remember our new WhenCalled overloads listed in the previous post:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public static IPublicNonVoidMethodHandler WhenCalled(Func func)
public static IPublicNonVoidMethodHandler WhenCalled(Func func); 
public static IPublicNonVoidMethodHandler WhenCalled(Func func);
…&lt;/pre&gt;

&lt;p&gt;If for each parameterized Func delegate we provide an additional overload that takes a predicate delegate, then we enable custom checkers.&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public static IPublicNonVoidMethodHandler WhenCalled(Func func);
public static IPublicNonVoidMethodHandler WhenCalled(Func func)
public static IPublicNonVoidMethodHandler WhenCalled(Func func, Func predicate)
public static IPublicNonVoidMethodHandler WhenCalled(Func func)
public static IPublicNonVoidMethodHandler WhenCalled(Func func, Func predicate)
…&lt;/pre&gt;

&lt;p&gt;Now look at that tests we can write now:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;[Test, Isolated]
public void TestWithCustomCheckers()
{
    Isolate.WhenCalled((int x, int y) =&amp;gt; MyClass.GetSum(x, y), (x, y) =&amp;gt; x &amp;gt; 0).WillReturn(6);
    Assert.AreEqual(6, MyClass.GetSum(1, 2));
    Assert.AreEqual(1, MyClass.GetSum(-1, 2));

    Isolate.WhenCalled((int x, int y) =&amp;gt; MyClass.GetSum(x, y), (x, y) =&amp;gt; x &amp;gt; 10 &amp;amp;&amp;amp; y &amp;gt; 10).WillReturn(6);
    Assert.AreEqual(6, MyClass.GetSum(20, 20));
    Assert.AreEqual(10, MyClass.GetSum(5, 5));
 
    Isolate.WhenCalled((int y) =&amp;gt; MyClass.GetSum(1, y), y =&amp;gt; y == 3).WillReturn(7);
    Assert.AreEqual(7, MyClass.GetSum(1, 3));
    Assert.AreEqual(3, MyClass.GetSum(1, 2));
 
    Isolate.WhenCalled((int x) =&amp;gt; MyClass.Elements[x], x =&amp;gt; x == 10).WillReturn(9);
    Assert.AreEqual(9, MyClass.Elements[10]);
 
    Isolate.WhenCalled((string x, string y) =&amp;gt; MyClass.Concatenate(x, y), (x, y) =&amp;gt; x.StartsWith(&amp;quot;abc&amp;quot;) &amp;amp;&amp;amp; y.EndsWith(&amp;quot;xyz&amp;quot;)).WillReturn(&amp;quot;bingo!&amp;quot;);
    Assert.AreEqual(&amp;quot;bingo!&amp;quot;, MyClass.Concatenate(&amp;quot;abc&amp;quot;, &amp;quot;xyz&amp;quot;));
    Assert.AreEqual(&amp;quot;ab&amp;quot;, MyClass.Concatenate(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;));
}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;What do we gain with this syntax?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1. I think it is more readable. In fact, I think it is &lt;em&gt;much&lt;/em&gt; more readable. Compare the line 19 with something like Isolate.WhenCalled(MyClass.Concatenate(null, null)).AndArgumentsMatch(1, Arg.StartsWith(&amp;quot;abc&amp;quot;)).AndArgumentsMatch(2, Arg.EndsWith(&amp;quot;xyz&amp;quot;)).WillReturn(&amp;quot;bingo!&amp;quot;).&lt;/p&gt;

&lt;p&gt;2. I believe when we extend functionality, we should prefer using built-in language constructions over new methods definition. The advanage of Func&amp;lt;T, bool&amp;gt; delegate is that it is based on generics and if we use it to define predicates then the predicates will naturally support methods and properties of respective types. You can simply write “x == 10” or “x.StartsWith(“abc”)”.&lt;/p&gt;

&lt;p&gt;3. You can take advantage of boolean logic using built-in language operators. Writing “x &amp;gt; 0 &amp;amp;&amp;amp; y &amp;gt; 0” is equally simple as writing “x &amp;gt; 0 || y &amp;gt; 0”, but how would you specify the latter checker using method-based notation? AndArgumentsMatch(1, Arg.GreaterThan(0)).OrArgumentsMatch(2, Arg.GreaterThan(0)?&lt;/p&gt;

&lt;p&gt;There is one unanswered question from Eli: handing “ref” and “out” arguments. I need to think how this can be resolved.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=481340" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/TypeMock/default.aspx">TypeMock</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/unit+testing/default.aspx">unit testing</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/Isolator/default.aspx">Isolator</category></item><item><title>How I would match faked method's arguments if I was TypeMock Isolator</title><link>http://bloggingabout.net/blogs/vagif/archive/2009/03/12/how-i-would-match-faked-method-s-arguments-if-i-was-typemock-isolator.aspx</link><pubDate>Thu, 12 Mar 2009 20:43:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:481326</guid><dc:creator>Vagif Abilov</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/rsscomments.aspx?PostID=481326</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/vagif/commentapi.aspx?PostID=481326</wfw:comment><comments>http://bloggingabout.net/blogs/vagif/archive/2009/03/12/how-i-would-match-faked-method-s-arguments-if-i-was-typemock-isolator.aspx#comments</comments><description>&lt;p&gt;I enjoy compactness and clarity of TypeMock Isolator AAA syntax. But it still lacks certain features in comparison with the original RecordExpectation-based syntax. One of such features is support for exact match of method&amp;#39;s attributes. Here&amp;#39;s an example (I assume that MyClass is a static class):&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;Isolate.WhenCalled(() =&amp;gt; MyClass.GetSum(1, 2)).WillReturn(10);
Isolate.WhenCalled(() =&amp;gt; MyClass.GetSum(3, 4)).WillReturn(20);&lt;/pre&gt;

&lt;p&gt;At the time of writing (TypeMock Isolator version 5.2.2) this will not trigger return value of 10 on all calls to GetSum with arguments 1 and 2. Argument values are currently ignored. However, recently Isolator was enhanced with exact argument match for indexers:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalled(() =&amp;gt; MyClass.Elements[1]).WillReturn(10);
Isolate.WhenCalled(() =&amp;gt; MyClass.Elements[2]).WillReturn(20);&lt;/pre&gt;

&lt;p&gt;In the case above any subsequent call to MyClass.Elements with an index value of 2 will return 20.&lt;/p&gt;

&lt;p&gt;Needless to say that support for exact argument match is one of the most requested features for AAA syntax. And the wait is soon over - there will be new methods that will make it possible to require exact argument matching. But is it really necessary to define new methods for this purpose? And what about partial argument match? I think there is another way to go.&lt;/p&gt;

&lt;p&gt;Before I move on to my proposal, let me express first what does not make me fully satisfied with current TypeMock approach.&lt;/p&gt;

&lt;h4&gt;1. Indexers&amp;#39; and methods&amp;#39; arguments should not be handled differently.&lt;/h4&gt;

&lt;p&gt;I understand what was the motivation for exact match of arguments on indexers. Indexers are mostly used with arrays, so when you set different expectations on ar[1] and ar[2], you will most likely want Isolator to map indexes to corresponding values and always return values depending on the passed index. The problem however is that C# makes it very easy to implement indexers as alternatives for getters and setters. So calls MyClass.GetValue(1) and MyClass.Values[1] can mean the same, but when you set expectations on GetValue and Values, the first one will ignore the argument value, while the second one will memorize it.&lt;/p&gt;

&lt;h4&gt;2. All or nothing is not enough.&lt;/h4&gt;

&lt;p&gt;Alright, we are soon getting support for exact match of arguments. All arguments. But will it cover all needs? If you can come up with scenario when you need to fake a method with exact argument match, and you can come up with scenario when you need to fake a method with no argument match, then why can&amp;#39;t you have a scenario when you need to match just certain method arguments? I can clearly see some typical scenarios: for example, you may fake a method that receives as part of its input current time, generated GUIDs etc. We can of course ask if this is a good practice to make tests dependent on non-deterministic data, but don&amp;#39;t forget that Isolator can be successfully used not just for pure unit tests. Smoke tests and even integration tests can also be simplified by isolating some aspects, and once there is support for both exact match and no match of method arguments, there is nothing wrong with requesting a partial match.&lt;/p&gt;

&lt;p&gt;But how can it be provided? Extend ExactMatch or whatever the new method will be called with an overload that would take an array of argument ordinals? Cumbersome and hard to read and maintain.&lt;/p&gt;

&lt;h3&gt;Let&amp;#39;s use lambda expressions at its full strength!&lt;/h3&gt;

&lt;p&gt;One thing that makes TypeMock Isolator AAA syntax charming (although if I am not mistaken it was Moq that introduced it first) is its use of lambda expressions:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate(() =&amp;gt; SomeCall(...)).WillReturn(...);&lt;/pre&gt;

&lt;p&gt;First time I saw this syntax, I spent some time trying to understand what I can use instead of &amp;quot;()&amp;quot; to the left from &amp;quot;=&amp;gt;&amp;quot;. Maybe depending on the faked method&amp;#39;s signature I could sometimes write (x)? Or even (x,y)? Then I realized that this was not an option: static class Isolate supports two overloads of WhenCalled:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public static IVoidActionHandler WhenCalled(Action action); 
public static IPublicNonVoidMethodHandler&amp;lt;TResult&amp;gt; WhenCalled&amp;lt;TResult&amp;gt;(Func&amp;lt;TResult&amp;gt; func)&lt;/pre&gt;

&lt;p&gt;So no matter whether you fake a void method or a method that returns some value, in both cases you pass a parameterless delegate. You can&amp;#39;t have unbound variables: all method&amp;#39;s arguments must be resolved.&lt;/p&gt;

&lt;p&gt;But what if WhenCalled supported overload for each form of Action and Func? Like this:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public static IPublicNonVoidMethodHandler WhenCalled(Func func) {...}
public static IPublicNonVoidMethodHandler WhenCalled(Func func)  {...}
public static IPublicNonVoidMethodHandler WhenCalled(Func func) {...} 
public static IPublicNonVoidMethodHandler WhenCalled(Func func) {...} 
public static IPublicNonVoidMethodHandler WhenCalled(Func func) {...}&lt;/pre&gt;

&lt;p&gt;This would open quite interesting way of setting expectations with exact match of only selected arguments! Because then we could write Isolate.WhenCalled(() =&amp;gt; ..., Isolate.WhenCalled((x) =&amp;gt; ..., Isolate.WhenCalled((x, y) =&amp;gt; .... And use these variables in a method call to indicate the unspecified arguments that will not be matched exactly.&lt;/p&gt;

&lt;p&gt;Let me illustrate this with an example. Let&amp;#39;s say we have a static class MyClass with a method GetSum(int x, int y). In TypeMock Isolator the following code will work fine:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalled(() =&amp;gt; MyClass.GetSum(1, 2)).WillReturn(4);
Assert.AreEqual(4, MyClass.GetSum(1, 2));
Assert.AreEqual(4, MyClass.GetSum(10, 20));&lt;/pre&gt;

&lt;p&gt;The code above uses currently supported syntax, except that I believe it&amp;#39;s semantics should be identical to the one used by indexers: method arguments should be matched exactly.&lt;/p&gt;

&lt;p&gt;And here how we can disregard argument values:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalled((int x, int y) =&amp;gt; MyClass.GetSum(x, y)).WillReturn(6);
Assert.AreEqual(6, MyClass.GetSum(1, 2));
Assert.AreEqual(6, MyClass.GetSum(2, 1));&lt;/pre&gt;

&lt;p&gt;In the code above it does not matter what arguments are passed to GetSum: we used a new overload of WhenCalled (unfortunately only supported in this blog entry) that let us speficy what arguments should be treated as unbound.&lt;/p&gt;

&lt;p&gt;And what about partial argument match? Now it&amp;#39;s easy:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalled((int y) =&amp;gt; MyClass.GetSum(1, y)).WillReturn(7);
Assert.AreEqual(7, MyClass.GetSum(1, 2));
Assert.AreEqual(4, MyClass.GetSum(2, 2));&lt;/pre&gt;

&lt;p&gt;Here Isolator is instructed to set expectations only on calls with first argument equal to 1. So GetSum(2,2) will not be faked.&lt;/p&gt;

&lt;p&gt;Finally setting expectations on an indexer:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;Isolate.WhenCalled(() =&amp;gt; MyClass.Elements[0]).WillReturn(8);
Assert.AreEqual(8, MyClass.Elements[0]);
Assert.AreNotEqual(8, MyClass.Elements[123]);
Isolate.WhenCalled((int x) =&amp;gt; MyClass.Elements[x]).WillReturn(9);
Assert.AreEqual(9, MyClass.Elements[123]);&lt;/pre&gt;

&lt;h3&gt;What do we gain by this?&lt;/h3&gt;

&lt;p&gt;1. Consistent semantics for argument match on indexers and methods.&lt;/p&gt;

&lt;p&gt;2. Support for partial argument match.&lt;/p&gt;

&lt;p&gt;3. No need to introduce new method names - just overloads to WhenCalled.&lt;/p&gt;

&lt;p&gt;4. And isn&amp;#39;t such use of labmda expressions cool?&lt;/p&gt;

&lt;p&gt;To demonstrate the proposed syntax, I wrote a simple example with unit tests that include the code I showed above. Of course I could not use &lt;em&gt;real&lt;/em&gt; TypeMock Isolator in these tests: it does not support (yet!) proposed WhenCalled overloads. So I cheated a little and implemented a toy MyIsolate class with just enough overloaded methods to make these example work. The test project can be downloaded from this post. You can compile and run the tests - they will pass as if they were real. I wish they were. &lt;/p&gt;

&lt;p&gt;&lt;a href="http://bloggingabout.net/blogs/vagif/archive/2009/03/14/typemock-isolator-and-matching-faked-method-s-arguments-part-2.aspx"&gt;Part 2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://bloggingabout.net/blogs/vagif/archive/2009/03/14/typemock-isolator-and-matching-faked-method-s-arguments-part-3.aspx"&gt;Part 3&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=481326" width="1" height="1"&gt;</description><enclosure url="http://bloggingabout.net/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.48.13.26/ArgumentBindingTests.zip" length="3038" type="application/x-zip-compressed" /><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/TypeMock/default.aspx">TypeMock</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/unit+testing/default.aspx">unit testing</category><category domain="http://bloggingabout.net/blogs/vagif/archive/tags/Isolator/default.aspx">Isolator</category></item></channel></rss>