xUnit Moq setup 在传入对象时检测方法失败

xUnit Moq setup failed to detect the method when a object is passed in

我对单元测试和最小起订量还很陌生。在我的 .net core 3.1 项目中,我使用 xUnit 和 Moq 编写单元测试。我有以下情况,我无法弄清楚为什么最小起订量无法检测到我的功能。

我的单元测试配置如下,

        VASTest t = new VASTest()
        {
            RecurringAndOneOffChargeID = 2
        };

        _dataserviceMock.Setup(x => x.CreateVASBillingRunRecurringChargesTest(t))
            .ReturnsAsync(() => true);

        _dataserviceMock.Setup(x => x.CreateVASBillingRunRecurringChargesTest(2))
            .ReturnsAsync(() => true);  

在我的测试函数中,我尝试用上面的设置来模拟两个函数,

         var result1 = _VASBillingDataAccess.CreateVASBillingRunRecurringChargesTest(tvt).Result;
         var result2 = _VASBillingDataAccess.CreateVASBillingRunRecurringChargesTest(2).Result;

我的模型受到打击 class,

public class VASTest
{
    public int RecurringAndOneOffChargeID { get; set; }
}

当我运行进行单元测试时,result1总是false,而result2总是true。

你能给我一些如何修复结果 1 的建议吗?

谢谢。

您的设置:

VASTest t = new VASTest()
{
  RecurringAndOneOffChargeID = 2
};

_dataserviceMock.Setup(x => x.CreateVASBillingRunRecurringChargesTest(t)).ReturnsAsync(() => true);

仅当 VASTest - t - 的实例在 SUT 调用中使用时才有效,或者您为 VASTest class 实现了自己的 equals 方法如果 VASTest 的 2 个实例彼此相等,则可以解析。如果其中任何一个不是这种情况,那么您将通过引用执行相等性检查并且不会得到满足。您使用 int (2) 的其他设置作为 int 是一种值类型,并且始终对值 iself 进行相等性检查。

如果我没有实施 IEquatable<T>

,我会执行以下操作
_dataserviceMock.Setup(x => 
    x.CreateVASBillingRunRecurringChargesTest(
        It.Is<VASTest>(t => t.RecurringAndOneOffChargeID.Equals(2))))
    .ReturnsAsync(() => true);