<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andrey Shchekin &#187; Fluent</title>
	<atom:link href="http://blog.ashmind.com/category/fluent/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ashmind.com</link>
	<description></description>
	<lastBuildDate>Mon, 15 Mar 2010 18:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>NMock 3.5</title>
		<link>http://blog.ashmind.com/2007/09/23/nmock-35/</link>
		<comments>http://blog.ashmind.com/2007/09/23/nmock-35/#comments</comments>
		<pubDate>Sun, 23 Sep 2007 09:09:14 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Fluent]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[NMock2]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/09/23/nmock-35/</guid>
		<description><![CDATA[One of the places where I am likely to actively use expression trees is testing/mocking. (I am not the first one to notice this — I saw some post on expression tree asserts, but I lost it). In my day-to-day development I am using NMock2. Ayende&#8216;s Rhino Mocks may be more convenient, but I do [...]]]></description>
			<content:encoded><![CDATA[<p>One of the places where I am likely to actively use expression trees is testing/mocking.<br />
(I am not the first one to notice this — I saw some post on expression tree asserts, but I lost it).</p>
<p>In my day-to-day development I am using <a href="http://nmock.org">NMock2</a>.<br /> <a href="http://www.ayende.com/Blog/">Ayende</a>&#8216;s <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a> may be more convenient, but I do not like the syntax.<br /> I just can&#8217;t read &#8220;expect call return&#8221; — my mind wants &#8220;expect call returns&#8221;.<br /> Also, event support in Rhinos is really cryptic.<br /> (I understand that it may be the only stringless way to express it).</p>
<p>So working on C# 3.0 project, I&#8217;ve looked into a stringless experience with NMock2.<br /> What I wanted was to pass a method call expression, to change this:
<pre class="csharpcode">
<span class="cls">Expect</span>.Once.On(fs)
    .Method(<span class="str">"GetDescendantDirectories"</span>)
    .With(RootPath)
    .Will(<span class="cls">Return</span>.Value(directories));</pre>
<p>into this
<pre class="csharpcode">
<span class="cls">Expect</span>.Once
    .That(() =&gt; fs.GetDescendantDirectories(RootPath))
    .Will(<span class="cls">Return</span>.Value(directories));</pre>
</p>
<p>Fortunately, it was quite easy to do with extension methods.<br />
I have spent more time figuring better fluent syntax than actually writing it.</p>
<p>But the case where expression trees really shine, I think, is in the possibility to do this:</p>
<pre class="csharpcode">
<span class="cls">Expect</span>.Exactly(directory.Files.Count)
    .That(() =&gt; access.IsPublicFile(<em><span class="cls">Any</span>.String</em>))
    .Will(<span class="cls">Return</span>.Value(<span class="kwrd">true</span>));</pre>
<p>I can analyze expression tree and find out that Any.String is not &#8220;a string&#8221;, but &#8220;any string&#8221;.<br />
It is also easy to imagine Any.String.Matching(&#8220;^whatever&#8221;) and so on.<br />
I have <strong>not</strong> tried to implement it, but I feel that it would be simple as well.
</p>
<p>Also, while writing the post, I just got an idea — in the world of extenson methods we can do</p>
<pre class="csharpcode">
<span class="cls">Expect</span>.Exactly(directory.Files.Count)
    .That(() =&gt; access.IsPublicFile(<span class="cls">Any</span>.String))
    .Will<em>.Return(<span class="kwrd">true</span>)</em>;</pre>
<p>without sacrificing extensibility.<br />
&#8220;Will&#8221; can also be chained if more than one action is needed (however, a delegate would be better for this).</p>
<p>It would be nice to actually add this to NMock2 (as 3.5 branch), but they are using CVS, not SVN.<br />
Also, am I the only one who thinks that SourceForge is slow and has hardly usable (bloated) design?</p>
<p>So if you want the code, you can ask me in comments, but it is primitive indeed.</p>
<style type="text/css">
.codewrap pre { width: 1%; }
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .cls { color: #2b91af; }
.csharpcode em { font-weight: normal; font-style: normal; text-decoration: underline; }
</style>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/09/23/nmock-35/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

