如果使用设置,最小起订量模拟调用 returns null

Moq mocked call returns null if using setup

我正在使用 Moq 为 C# 应用程序编写测试。我的测试初始化​​程序具有以下代码:

UnityContainer unityContainer = new UnityContainer();

_serviceMock = new Mock<IService>();
_serviceMock.Setup(mock => mock.GetSearchInfoAsync(It.IsAny<CancellationToken>(), It.IsAny<IEnumerable<string>>(), It.IsAny<identifierType>(), It.IsAny<bool>())).Callback(() => _count++);
unityContainer.RegisterInstance(typeof(IService), _serviceMock.Object, new ContainerControlledLifetimeManager());

我想测试一次调用是否只调用一次。我正在这样尝试:

int _count = 0;

[TestMethod]
public void Properties_Test()
{
    _serviceMock.Verify(mock => mock.GetSearchInfoAsync(It.IsAny<CancellationToken>(), It.IsAny<IEnumerable<string>>(), It.IsAny<identifierType>(), It.IsAny<bool>()), Times.Exactly(1), "Invocation was performed " + _count + " times but was expected only once!");
}

这是实际调用的方法:

private void Search(string queryValue, identifierType identifierType)
{
    CancellationToken cancellationToken;

    lock (_syncLock)
    {
        _cancellationTokenSource.Cancel();
        _cancellationTokenSource = new CancellationTokenSource();
        cancellationToken = _cancellationTokenSource.Token;
    }

    IService Service = ServiceLocator.Current.GetInstance<IService>();

    Service.GetSearchInfoAsync(cancellationToken, new[] {queryValue}, identifierType)
        .ContinueWith(
            task =>
            {
                // Do stuff
            }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default);
}

问题是,如果我使用上面详述的这一行,

_serviceMock.Setup(mock => mock.GetSearchInfoAsync(It.IsAny<CancellationToken>(), It.IsAny<IEnumerable<string>>(), It.IsAny<identifierType>(), It.IsAny<bool>())).Callback(() => _count++);

this returns null 并生成 NullPointerException:

Service.GetSearchInfoAsync(cancellationToken, new[] {queryValue}, identifierType)

但是,如果我注释掉该行,测试 运行 没问题(尽管不计算调用次数)。

我做错了什么?这是我第一次为此使用 Moq,据我所知我已经正确实现了计数功能。

编辑: 根据 Chris Sinclair 的建议,我将初始化程序更改为此,从而解决了问题:

UnityContainer unityContainer = new UnityContainer();

_serviceMock = new Mock<IService>();
Task<IEnumerable<ISearchResult>> task = new Task<IEnumerable<ISearchResult>>(Enumerable.Empty<ISearchResult>);
_serviceMock.Setup(mock => mock.GetSearchInfoAsync(It.IsAny<CancellationToken>(), It.IsAny<IEnumerable<string>>(), It.IsAny<identifierType>(), It.IsAny<bool>())).Returns(task).Callback(() => _count++);
unityContainer.RegisterInstance(typeof(IService), _serviceMock.Object, new ContainerControlledLifetimeManager());

当您 "Setup" 方法时,您设置了一个回调但没有提供 return 值。因此,当调用模拟方法时,它将 return return 类型的默认值(在这种情况下, Task<> 类型将导致 null return 值)。因此,当您的 Search 方法调用您的模拟 GetSearchInfoAsync 方法时,它会收到一个 null 引用,当它稍后尝试调用 .ContinueWith 时自然会失败。

尝试添加一个 .Returns() 来为您的模拟方法提供一个虚拟 Task<>

_serviceMock.Setup(mock => mock.GetSearchInfoAsync(It.IsAny<CancellationToken>(), It.IsAny<IEnumerable<string>>(), It.IsAny<identifierType>(), It.IsAny<bool>()))
    .Returns(new Task<IEnumerable<ISearchResult>>(Enumerable.Empty<ISearchResult>))
    .Callback(() => _count++);