ฉันมีคลาสนี้พร้อมกับตัวสร้างของมัน
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;
}
}
ฉันได้สร้างการทดสอบหน่วย xunit เพื่อตรวจสอบความเท่าเทียมกันของวัตถุ
BankAccountTest ระดับสาธารณะ { BankAccount ส่วนตัว _bankAccount;
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 }
ฉันลองด้วย Assert.True(accountExpected.Equals(_bankAccount));
แล้ว แต่ก็ยังไม่ประสบความสำเร็จ
xunit compare two objects
buildplease.com/pages/testing-deep-equalilty - person mjwills   schedule 16.06.2020accountExpected.Should().BeEquivalentTo(_bankAccount);
. แต่ฉันยังนึกไม่ออกว่าจะแก้ปัญหาอย่างไรหากไม่มี FluentAssertions - person Steve   schedule 16.06.2020