FakeItEasy:返回 NULL 返回 fakeiteasy null 对象,因此 if 语句总是通过检查

问题描述 投票:0回答:1

所以我有一个 xunit 测试方法,它进入我的 Productorderservice 来检查返回值是否为 null。然而,返回的对象永远不会为 null,因为它返回一个 fakeiteasy null 对象,即使我指定 Fakeiteasy 的 .Returns 方法返回 null。

var newproductionOrder = await _productionOrderRepository.CreateProductionOrder(newProduct,productionOrder);

if (newproductionOrder != null)
{
    await _productionOrderRepository.UpdateCounter();
    await _productRepository.UpdateCounter();
    return newproductionOrder;
}
throw new Exception("Failed to Create ProductionOrder");

我的测试代码片段

A.CallTo(() => fakePORepository.CreateProductionOrder(
        allProducts[0], invalidProductionOrder))
    .Returns(nullProductionOrder);

空生产订单是通过执行以下操作创建的

public static ProductionOrderDto? CreateNullProductionOrderDto()
{
    return null;
}

我在这里做错了什么?

尝试使用惰性返回来指定返回null的自定义方式。但它仍然返回 fakeiteasy 对象,并且我的 if 语句在它不必运行时运行。

c# .net-core xunit fluent-assertions fakeiteasy
1个回答
0
投票

没有看到

allProducts[0
]
and 
invalidProductionOrder
are defined, it's impossible to say. But your code generally looks good, so the most likely explanation is that the
Equals` 方法的类型是基于引用的,而不是基于值的,并且传入的对象与您用于配置的实例不完全相同假货。

请参阅参数约束 - 精确匹配值,了解 FakeItEasy 的“默认”参数比较的说明。

调试通过。验证

Equals
方法是否会为每个参数返回 true。

© www.soinside.com 2019 - 2024. All rights reserved.