Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xUnit比较对象相等性_C#_Xunit - Fatal编程技术网

C# xUnit比较对象相等性

C# xUnit比较对象相等性,c#,xunit,C#,Xunit,我有这个类及其构造函数 public class BankAccount { public int Id { get; private set; } public int BankAccountNo { get; private set; } public decimal Balance { get; private set; } public BankAccount(int BankAccountNo, decimal Balance) {

我有这个类及其构造函数

 public class BankAccount
    {
    public int Id { get; private set; }
    public int BankAccountNo { get; private set; }
    public decimal Balance { get; private set; }

    public BankAccount(int BankAccountNo, decimal Balance)
    {
        this.BankAccountNo = BankAccountNo;

        if(Balance <= 0)
        {
            throw new ArgumentException("Create bank account failed. Balance should be more than zero.");
        }

        this.Balance = Balance;
    }
}

我尝试使用
Assert.True(accountExpected.Equals(_bankAccount))但仍然没有成功。

断言失败,因为您正在比较内存中的对象引用位置。相反,您应该在对象内部的属性上进行断言


.

断言失败,因为您正在比较内存中的对象引用位置。相反,您应该在对象内部的属性上进行断言


.

您正在尝试比较两个不可
相等的对象
!在C#中,为了能够比较两个对象,它们需要是
相等的
,换句话说,基础类必须实现,如果您没有为引用类型实现
IEquatable
,相等性检查只会检查对象引用!(请注意,对于值类型,此行为是不同的!)

因此,您需要做的是以下任一操作-

  • 通过以下方式为您的
    后台帐户
    类实现
    IEquatable
  • 或者,您可以逐个比较各个字段:
  • Assert.Equal(bankAccount1.Balance,bankAccount2.Balance)
    
    这里还有一篇关于平等断言的好博文:


    您试图比较两个不可
    相等的对象
    !在C#中,为了能够比较两个对象,它们需要是
    相等的
    ,换句话说,基础类必须实现,如果您没有为引用类型实现
    IEquatable
    ,相等性检查只会检查对象引用!(请注意,对于值类型,此行为是不同的!)

    因此,您需要做的是以下任一操作-

  • 通过以下方式为您的
    后台帐户
    类实现
    IEquatable
  • 或者,您可以逐个比较各个字段:
  • Assert.Equal(bankAccount1.Balance,bankAccount2.Balance)
    
    这里还有一篇关于平等断言的好博文:


    这是否回答了您的问题。您始终可以对它们进行序列化,并检查结果stringsGoogle中的
    xunit比较两个对象。谢谢使用FluentAssertions解决了这个问题<代码>accountExpected.Should().BeEquivalentTo(\u bankAccount)。但是,我还不知道如何在没有FluentAssertions的情况下解决它。这能回答你的问题吗。您始终可以对它们进行序列化,并检查结果stringsGoogle中的
    xunit比较两个对象。谢谢使用FluentAssertions解决了这个问题<代码>accountExpected.Should().BeEquivalentTo(\u bankAccount)。但是,我还不知道如何在没有FluentAssertions的情况下解决它。
    
        public BankAccountTest()
        {
            _bankAccount = new BankAccount();
        }
    
        [Theory, MemberData(nameof(BankAccountConstructorShouldPass_Data))]
        public void BankAccountConstructorShouldPass(BankAccount account, BankAccount accountExpected)
        {
            // Act
            _bankAccount = new BankAccount(account.BankAccountNo, account.Balance);
    
            // Assert
            Assert.Equal<BankAccount>(accountExpected,_bankAccount);
        }
    
        public static TheoryData<BankAccount, BankAccount> BankAccountConstructorShouldPass_Data()
        {
            return new TheoryData<BankAccount, BankAccount>
            {
                {
                    new BankAccount(123, 250.00M),        
                    new BankAccount(123, 250.00M)
                },
                {
                    new BankAccount(321, 150.50M),       
                    new BankAccount(321, 150.50M)
                }
            };
        }
    }
    
    Assert.Equal() Failure
    Expected: BankAccount { Balance = 250.00, BankAccountNo = 123, Id = 0 }
    Actual:   BankAccount { Balance = 250.00, BankAccountNo = 123, Id = 0 }