XUnit statement for checking equality of objects

I use the XUnit framework to test C # code.

Is there any assert method available in this structure that performs object comparison? My intention is to verify the equality of each of the public and private variables of the object.

I tried these alternatives, but it rarely works:

1) bool IsEqual = (Obj1 == Obj2)
2) Assert.Same(Obj1, Obj2) which I couldnt understand what happens internally
+24
source share
5 answers

To do this, you need to have your own comparator when you are comparing objects, otherwise they are checked based on whether they refer to the same object in memory. To override this behavior, you need to override the method Equalsand GetHashCode, and then you can do the following:

Assert.True(obj1.Equals(obj2));

MSDN Equals: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

: IEquatable Object.Equals()?

+14

, , ,

using Newtonsoft.Json;

json-, .

var obj1Str = JsonConvert.SerializeObject(obj1);
var obj2Str = JsonConvert.SerializeObject(obj2);
Assert.Equal(obj1Str, obj2Str );
+36

NuGet, . , .

  1. DeepEqual:

    object1.ShouldDeepEqual(object2);
    
  2. ExpectedObjects:

    [Fact]
    public void RetrievingACustomer_ShouldReturnTheExpectedCustomer()
    {
      // Arrange
      var expectedCustomer = new Customer
      {
        FirstName = "Silence",
        LastName = "Dogood",
        Address = new Address
        {
          AddressLineOne = "The New-England Courant",
          AddressLineTwo = "3 Queen Street",
          City = "Boston",
          State = "MA",
          PostalCode = "02114"
        }                                            
      }.ToExpectedObject();
    
    
      // Act
      var actualCustomer = new CustomerService().GetCustomerByName("Silence", "Dogood");
    
      // Assert
      expectedCustomer.ShouldEqual(actualCustomer);
    }
    
+7

, , , , ( , xunit 2.3.1 .net Core 2.0).

, , .Equal, IEqualityComparer<T> . , .

: Assert.Equal(expectedParameters, parameters, new CustomComparer<ParameterValue>());

XUnit, -, , , EqualException , XUnit .

public class CustomComparer<T> : IEqualityComparer<T>
{
    public bool Equals(T expected, T actual)
    {
        var props = typeof(T).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
        foreach (var prop in props)
        {
            var expectedValue = prop.GetValue(expected, null);
            var actualValue = prop.GetValue(actual, null);
            if (!expectedValue.Equals(actualValue))
            {
                throw new EqualException($"A value of \"{expectedValue}\" for property \"{prop.Name}\"",
                    $"A value of \"{actualValue}\" for property \"{prop.Name}\"");
            }
        }

        return true;
    }

    public int GetHashCode(T parameterValue)
    {
        return Tuple.Create(parameterValue).GetHashCode();
    }
}

: , != ( , , , ). , .Equals , , , .

+6

FluentAssertions .

myObject.ShouldBeEquivalentTo(new { SomeProperty = "abc", SomeOtherProperty = 23 });

"myObject". , .

+1

All Articles