编程语言
首页 > 编程语言> > c#-如果使用设置,Moq模拟调用将返回null

c#-如果使用设置,Moq模拟调用将返回null

作者:互联网

我正在使用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++);

这将返回null并生成NullPointerException:

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

但是,如果我注释掉那条线,则测试运行良好(尽管不计算调用次数).

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

编辑:按照克里斯·辛克莱尔的建议,我将初始化程序更改为此,从而解决了此问题:

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());

解决方法:

当您“设置”该方法时,您设置了一个回调,但没有提供返回值.这样,当调用模拟方法时,它将返回返回类型的默认值(在这种情况下,Task<>类型将导致返回空值).这样,当您的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++);

标签:unit-testing,mocking,moq,c
来源: https://codeday.me/bug/20191119/2035272.html