Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You would need to override <code>.Equals()</code> so that it compared <code>properties</code>, then use the</p> <p><code>Assert.AreEqual Method (Object, Object)</code>. Please see below:</p> <p><a href="https://stackoverflow.com/questions/318210/compare-equality-between-two-objects-in-nunit">Compare equality between two objects in NUnit</a></p> <p>You would want to implement such a thing in your model itself.</p> <pre><code>// useful class public class MyStuff { public int Id { get; set; } public string Name { get; set; } public int MyValue { get; set; } public override int GetHashCode() { return Id; } public override bool Equals(object obj) { if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != typeof (MyStuff)) return false; var other = obj as MyStuff; return (other.Id == Id &amp;&amp; other.MyValue == MyValue &amp;&amp; other.Equals(other.Name, Name)); // use .Equals() here to compare objects; == for Value types // alternative weak Equals() for value objects: // return (other.MyValue == MyValue &amp;&amp; other.Equals(other.Name, Name) ); } } </code></pre> <p><strong>EDIT:</strong> In retrospect, I have decided that having a duplication of properties in your viewmodel and model is probably a bad pattern and is part of the reason you are having so many testing issues. Rather, you should allow your ViewModel to wrap your model.</p> <pre><code>public class CustomerViewModel { // This is the same as CustomerModel.CustomerName, but the names differ public CustomerModel CustomerModel { get; set; } pubiic CustomerViewModel() { CustomerModel = new CustomerModel(); } } public class CustomerModel { public string CustomerName { get; set; } public int ID { get; set; } } </code></pre> <p>At that point it's much easier to test it since you have your wrapped model that you can compare to a new copy of the same model, using the .Equals override pattern. At the end of the day I just don't think that trying to come up with a magic bullet "compare any model to any model" is a good idea, nor is it practical.</p>
 

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