如何在 nunit 中最小化惰性接口?

How to Moq lazy interface in nunit?

被测系统

public class AddDateRangeSaga : IDistributedSaga<AddDateRangeRequestModel, AddDateRangeResponseModel>
{
    /// <summary>
    /// AddDateRangeAr Inject
    /// </summary>
    public readonly Lazy<IAddDateRangeAr> _addDateRangesAr;

    /// <summary>
    /// For log and exception handling
    /// </summary>
    private readonly IApiRequestHandler _reqHandler;

    public AddDateRangeSaga(IApiRequestHandler reqHandler
        , Lazy<IAddDateRangeAr> addDateRangesAr)
    {
        _reqHandler = reqHandler;
        _addDateRangesAr = addDateRangesAr;
    }
    public async Task<AddDateRangeResponseModel> Execute(AddDateRangeRequestModel request)
    {
        return await _addDateRangesAr.Value.AddDateRange(request);
    }
}

测试用例

public class AddDateRangeSagaTests
{
    private Mock<IApiRequestHandler> _mockApiRequestHandler;
    public Mock<Lazy<IAddDateRangeAr>> _addDateRangesAr;
    private MockRepository mockRepository;

    [SetUp]
    public void SetUp()
    {
        mockRepository = new MockRepository(MockBehavior.Strict);
        _mockApiRequestHandler = mockRepository.Create<IApiRequestHandler>();
        _addDateRangesAr = mockRepository.Create<Lazy<IAddDateRangeAr>>();
    }

    private AddDateRangeSaga CreateAddDateRangeSagaTests()
    {
        return new AddDateRangeSaga(this._mockApiRequestHandler.Object, _addDateRangesAr.Object);
    }

    [Test]
    public async Task AddDateRangeSaga_StateUnderTest_ExpectedBehavior()
    {
        //Arrange
        var addDateRangeAr = this.CreateAddDateRangeSagaTests();
        AddDateRangeRequestModel addDateRangeRequestModel = new AddDateRangeRequestModel();
        AddDateRangeResponseModel addDateRangeResponseModel = new AddDateRangeResponseModel();
        _addDateRangesAr.Setup(x => x.Value.AddDateRange(addDateRangeRequestModel)).ReturnsAsync(addDateRangeResponseModel);
       
        var dd = addDateRangeAr.Execute(addDateRangeRequestModel);
        
        //Assert
        Assert.AreEqual(1,1); //dummy

    }
}

执行过程中收到错误

System.NotSupportedException : Unsupported expression: x => x.Value Non-overridable members (here: Lazy.get_Value) may not be used in setup / verification expressions.

问题

我无法找出惰性最小起订量的确切问题。有人可以帮我解决这个问题吗?

先给大家分享一下修改后的测试版本再说明。

public class AddDateRangeSagaTests
{
    private Mock<IApiRequestHandler> apiRequestHandlerMock;
    private Mock<IAddDateRangeAr> addDateRangesArMock;
    private MockRepository mockRepository;
    
    [SetUp]
    public void SetUp()
    {
        mockRepository = new MockRepository(MockBehavior.Strict);
        apiRequestHandlerMock = mockRepository.Create<IApiRequestHandler>();
        addDateRangesArMock = mockRepository.Create<IAddDateRangeAr>();
    }

    private AddDateRangeSaga CreateAddDateRangeSagaTests()
    {
        var lazyAddDateRangesAr = new Lazy<IAddDateRangeAr>(() => addDateRangesArMock.Object);
        return new AddDateRangeSaga(apiRequestHandlerMock.Object, lazyAddDateRangesAr);
    }
    
    [Test]
    public async Task AddDateRangeSaga_StateUnderTest_ExpectedBehavior()
    {
        //Arrange
        var sut = this.CreateAddDateRangeSagaTests();
        addDateRangesArMock
          .Setup(x => x.AddDateRange(It.IsAny<AddDateRangeRequestModel>()))
          .ReturnsAsync(new AddDateRangeResponseModel());

        //Act
        var response = await sut.Execute(addDateRangeRequestModel);

        //Assert
        Assert.NotNull(response);
    }
}
  • 我已将您的 _addDateRangesAr 字段更改为 addDateRangesArMock
    • 这里我们模拟的是接口本身而不是 Lazy 容器
    • 我也将可见性从 public 更改为 private,因为此字段不应被任何其他 class
    • 访问
  • 我已将 _mockApiRequestHandler 字段的名称更改为 apiRequestHandlerMock 以避免使用 _ 前缀
  • CreateAddDateRangeSagaTests 中我已经初始化了一个 Lazy<IAddDateRangeAr> 它将 return 模拟 IAddDateRangeAr 每当它第一次被访问时
  • 我简化了模拟设置并使其更通用
    • 来自 .AddDateRange(addDateRangeRequestModel)
    • .AddDateRange(It.IsAny<AddDateRangeRequestModel>())
  • 我已经用 await 调用了 Execute 以便能够检查响应
  • 我使用了一个简单的 null 检查来确保它按预期工作