<?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>Quickduck &#187; Equality</title> <atom:link href="http://quickduck.com/blog/tag/equality/feed/" rel="self" type="application/rss+xml" /><link>http://quickduck.com/blog</link> <description>Straight from the mind of geniuseseses....</description> <lastBuildDate>Mon, 09 Jan 2012 02:29:30 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Unit Testing Expression Tree Equality</title><link>http://quickduck.com/blog/2009/08/25/unit-testing-expression-tree-equality/</link> <comments>http://quickduck.com/blog/2009/08/25/unit-testing-expression-tree-equality/#comments</comments> <pubDate>Tue, 25 Aug 2009 03:31:02 +0000</pubDate> <dc:creator>Ben</dc:creator> <category><![CDATA[.Net]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[Unit Testing]]></category> <category><![CDATA[Comparison]]></category> <category><![CDATA[Equality]]></category> <category><![CDATA[Expression Trees]]></category> <category><![CDATA[Mocks]]></category> <category><![CDATA[Rhino]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=131</guid> <description><![CDATA[Sometimes you need to test whether two expressions are the same. However when you do a simple AreEqual() test on two expressions that look the same you get a negative result. This method will fail This looks like it should work however it doesn&#8217;t. This is because when using anonymous expressions/delegates the CLR does some [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2009/08/25/unit-testing-expression-tree-equality/"></g:plusone></div><p>Sometimes you need to test whether two expressions are the same. However when you do a simple AreEqual() test on two expressions that look the same you get a negative result.</p><p><strong>This method will fail</strong></p><pre class="brush: csharp; title: ; notranslate">
[TestMethod]
public void TestDoesNotWork()
{
  Expression&lt;Func&lt;int, int&gt;&gt; expressionA = x =&gt; x + 1;
  Expression&lt;Func&lt;int, int&gt;&gt; expressionB = x =&gt; x + 1;

  Assert.AreEqual(expressionA, expressionB);
}
</pre><p>This looks like it should work however it doesn&#8217;t. This is because when using anonymous expressions/delegates the CLR does some magic behind the scene to add a method on the fly, creating a new instance for every anonymous expression/delegate.</p><p>So if you have two expressions that are syntactically the same but not the same reference the easiest way to ensure they are the same is to compile the expression and invoke it comparing the resultant value.</p><pre class="brush: csharp; title: ; notranslate">
[TestMethod]
public void TestDoesWork()
{
  Expression&lt;Func&lt;int, int&gt;&gt; expressionA = x =&gt; x + 1;
  Expression&lt;Func&lt;int, int&gt;&gt; expressionB = x =&gt; x + 1;

  Assert.AreEqual(expressionA.Compile().Invoke(3), expressionB.Compile().Invoke(3));
}
</pre><p>An alternate approach would be to de-construct each constituent part of the expression trees comparing each part as you go. This however is far more complex.</p><p>For a more real world example that involves using Rhino mocks and Expect calls please read on:</p><p><span id="more-131"></span></p><p>When using Rhino Mocks and setting up an Expect call on a method that takes an expression you will most likely find that the Expect assertion always fails.</p><p><strong> The class that will be mocked out.</strong><br /> This class will have the method on it that takes an expression.</p><pre class="brush: csharp; title: ; notranslate">
public class Repository
{
  public virtual void Get&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; whereCondition)
  {
        // Actual logic.
   }
}
</pre><p><strong>The class we are unit testing</strong></p><pre class="brush: csharp; title: ; notranslate">
public class UserManager
{
  public Repository Repo { get; set; }

  public void GetUserById(int id)
  {
    Repo.Get&lt;User&gt;(x =&gt; x.Id == id);
  }
}
</pre><p><strong>Supporting classes</strong></p><pre class="brush: csharp; title: ; notranslate">
public class User
{
  public int Id { get; set;  }
  public int Name { get; set; }
}
</pre><p>For our unit test we want to test that when we call the GetUserById() method on the UserManager class that we Expect the Repository.Get<TEntity>() method will be called with an expected expression value.</p><p><strong>Unit Test</strong></p><pre class="brush: csharp; title: ; notranslate">
[TestMethod]
public void Test()
{
  // Arrange
  UserManager manager = new UserManager { Repo = MockRepository.GenerateMock&lt;Repository&gt;() };
  manager.Repo.Expect(r =&gt; r.Get&lt;User&gt;(x =&gt; x.Id == 3));

  // Act
  manager.GetUserById(3);

  // Assert
  manager.Repo.VerifyAllExpectations();
}
</pre><p>This looks like it should work however it won&#8217;t.</p><p>So using the approach listed earlier we can test that the method with an expression is called with the correct value by compiling and invoking the expected expression and the actual expression that was called and comparing their values.</p><pre class="brush: csharp; title: ; notranslate">
[TestMethod]
public void TestExpressionCalled()
{
  // Arrange
  UserManager manager = new UserManager { Repo = MockRepository.GenerateMock&lt;Repository&gt;() };

  // Act
  manager.GetUserById(3);

  // Assert
  Expression&lt;Func&lt;User, bool&gt;&gt; expected = x =&gt; x.Id == 3;
  Expression&lt;Func&lt;User, bool&gt;&gt; actual = (Expression&lt;Func&lt;User, bool&gt;&gt;)manager.Repo.GetArgumentsForCallsMadeOn(r =&gt; r.Get&lt;User&gt;(null))[0][0];

  User user = new User { Id = 3 };
  Assert.AreEqual(expected.Compile().Invoke(user), actual.Compile().Invoke(user));
}
</pre>]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2009/08/25/unit-testing-expression-tree-equality/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: quickduck.com @ 2012-02-05 17:33:10 -->
