Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit Testing Expression Trees
    primarykey
    data
    text
    <p>I recently need to build a Expression tree so I wrote a Test method like so...</p> <pre><code> /// &lt;summary&gt; /// /// &lt;/summary&gt; [TestMethod()] [DeploymentItem("WATrust.Shared.Infrastructure.dll")] public void BuildForeignKeysContainsPredicate_shoud_build_contains_predicate() { RemoteEntityRefLoader_Accessor&lt;ReferencedEntity&gt; target = CreateRemoteEntityRefLoader_Accessor(); List&lt;object&gt; foreignKeys = new List&lt;object&gt;() { 1, 2, 3, 4 }; Expression&lt;Func&lt;ReferencedEntity, bool&gt;&gt; expected = (ReferencedEntity referencedEntity) =&gt; foreignKeys.Contains(referencedEntity.Id); Expression&lt;Func&lt;ReferencedEntity, bool&gt;&gt; actual; actual = target.BuildForeignKeysContainsPredicate(foreignKeys, "Id"); Assert.AreEqual(expected.ToString(), actual.ToString()); } </code></pre> <p>When I finally got the "BuildForeignKeysContainsPredicate" method working I could never get teh test to pass... Here is the method:</p> <pre><code> /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="foreignKeys"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; private Expression&lt;Func&lt;TReferencedEntity, bool&gt;&gt; BuildForeignKeysContainsPredicate(List&lt;object&gt; foreignKeys, string primaryKey) { Expression&lt;Func&lt;TReferencedEntity, bool&gt;&gt; result = default(Expression&lt;Func&lt;TReferencedEntity, bool&gt;&gt;); try { ParameterExpression entityParameter = Expression.Parameter(typeof(TReferencedEntity), "referencedEntity"); ConstantExpression foreignKeysParameter = Expression.Constant(foreignKeys, typeof(List&lt;object&gt;)); MemberExpression memberExpression = Expression.Property(entityParameter, primaryKey); Expression convertExpression = Expression.Convert(memberExpression, typeof(object)); MethodCallExpression containsExpression = Expression.Call(foreignKeysParameter , "Contains", new Type[] { }, convertExpression); result = Expression.Lambda&lt;Func&lt;TReferencedEntity, bool&gt;&gt;(containsExpression, entityParameter); } catch (Exception ex) { throw ex; } return result; } </code></pre> <p>But the test fails every time, I switched the line <code>Assert.AreEqual(expected, actual);</code> to this: <code>Assert.AreEqual(expected.ToString(), actual.ToString());</code> I understand why it is failing because when you look at the results of the ToString method they are different.</p> <pre><code>Assert.AreEqual failed. Expected:&lt;referencedEntity =&gt; value(Shared.Infrastructure.Test.RemoteEntityRefLoaderTest+&lt;&gt;c__DisplayClass13).foreignKeys.Contains(Convert(referencedEntity.Id))&gt;. Actual :&lt;referencedEntity =&gt; value(System.Collections.Generic.List`1[System.Object] ) .Contains(Convert(referencedEntity.Id))&gt;. </code></pre> <p>I just don't understand why... Does any one have general tips on unit testing expressions and suggestions how to get my specific test to pass?</p> <p>Thanks...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload