c# - Unit Testing Expression Trees -


I just needed to create an expression tree, so I wrote a test method ...

  /// & lt; Summary & gt; ////// & lt; / Summary & gt; [TestMethod ()] [DeploymentItem ("WATrust.Shared.Infrastructure.dll")] Public Zero BuildForeignKeysContainsPredicate_shoud_build_contains_predicate () {RemoteEntityRefLoader_Accessor & LT; ReferencedEntity & gt; Target = createRemoteTrFloader_accessor (); & Lt; Object & gt; Foreigners = New list & lt; Object & gt; () {1, 2, 3, 4}; Expression & lt; Funk & lt; Referenced anti, boolean & gt; & Gt; Expected = (Referenced Entity Referenced Entity) = & gt; ForeignKeys.Contains (referencedEntity.Id); Expression & lt; Funk & lt; Referenced anti, boolean & gt; & Gt; Actual; Actual = Goal.BillionsCare accept content (Foreign keys, "ID"); Confirm. Precarious (expected toaster), actual. Toasting ()); }  

When I finally got the job of the "BuildOrganics Content Prize" method, I could never pass the test ... here's the method:

  // / & Lt; Summary & gt; ////// & lt; / Summary & gt; /// & lt; Ultimate name = "foreign kicks" & gt; & Lt; / Param & gt; /// & lt; Returns & gt; & Lt; / Returns & gt; Personal expression & lt; Funk & lt; Trencencedity, Ball & gt; & Gt; BuildForeignKeysContainsPredicate (list of & lt; Object & gt; foreignKeys, String PrimaryKey) {of the expression & lt; & Lt; TReferencedEntity, bool & gt; & Gt; Results = Default (expression & lt; funk & lt; TReferencedEntity, bool & gt; & gt;); {Try Parameter = Expression.Parameter (typeof (TransFredentity), "Reference Entity"); Continuous acceleration foreign kikaparameter = expression. Constant (foreign key, typef (list & object; object & gt;)); Member Experience Member Expression = Expression. Property (Entity Pmeter, Primer); Expression expression = expression = expression. Convert (Member Expression, Typef (Object)); Expression = expression in express = expression Call (foreign keypaymator, "included", new type [] {}, conversion expansion); Result = expression. Lambda & lt; Func & lt; Trenfensedty, Bullet & gt; & Gt; (Exercise, entity parameter); } Hold (Exceptional) {East; } Return results; }  

But the test fails every time, I close the line Assert.AreEqual (hopefully, real); To do this: Assert.AreEqual (expected (.toString), actual.ToString ()) ;. I understand why this is failing, because when you look at the results of the ToString method they are different

  Assert.AreEqual be unsuccessful. Expected: & lt; Referenced entity = & gt; Price (Shared.Infrastructure.Test.RemoteEntityRefLoaderTest + & lt; & gt; c__DisplayClass13) .foreignKeys.Contains (Convert (referencedEntity.Id)) & gt; Actual: & lt; ContextType = & gt; Value (System.Collections.Generic.List`1 [System.Object]). This includes (Convert (.initity.id)) & gt;  

I do not understand why ... does anyone get general tips on unit test expressions and suggestions on how to pass my specific test?

Thank you ...

Depending on the code you posted,

  • The expected value is an anonymous representative / method CLR does some magic behind the scene to add a method to the fly. In case if the method uses some local variables, CLR creates a new class with the fields set on these values, with the new anon method (method can access the local value value). So that a compiler gave a weird name so that it does not conflict with user-defined methods, it is your ..c_DisplayClass13,
  • The actual value is a expression returned by your method <. T & gt; .

And so .. the investigation of equality between the two fails. You need to compare the elements of the collection given by both of them. So I thought that ... for both lists (or better data structure), change the expectation and real values ​​of both, and then collect them of NUINT which collects the archive criteria.

Update : You got me to read the expression on the tree. +1 for this.
I will change my answer - Comparing expression trees through hack-and-atert is a delicate test (for example if MS will change the internal structure of an expression tree in the future) expression trees are just code blocks (As I have just found) which Func & lt; Tinput evaluates for the same result of TResult) - Therefore, my test should be expected and the actual code block should be given the same input and see if they distribute the same output. So my test will be for my

  Assert.AreEqual (expected.Compile () invoke (inputEntity), actual.Compile () invoke (inputEntity) ..);  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -