XUnit Assertion for checking equality of objects

后端 未结 5 858
小蘑菇
小蘑菇 2021-02-03 19:11

I am using XUnit framework to test my C# code.

Is there any assert method available in this framework which does the object comparison? My intention is to check for equa

5条回答
  •  伪装坚强ぢ
    2021-02-03 19:28

    There are NuGet packages that do this for you. Here are two examples that I personally use.

    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);
      }
      

提交回复
热议问题