简体   繁体   English

如果使用安装程序,Moq模拟调用返回null

[英]Moq mocked call returns null if using setup

I am writing tests for a C# application, using Moq. 我正在使用Moq为C#应用程序编写测试。 My test initialiser has the following code: 我的测试初始化​​程序具有以下代码:

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

I want to test that a call is made only once. 我想测试一次呼叫只进行一次。 I am trying it like this: 我这样想:

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!");
}

This is the method where it actually gets called: 这是实际调用它的方法:

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

The problem is that if I use this line as detailed above, 问题是,如果我如上所述使用此行,

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

this returns null and generates a NullPointerException: 这将返回null并生成NullPointerException:

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

However, if I comment out that line, the tests run fine (albeit not counting the number of calls). 但是,如果我注释掉该行,则测试运行正常(尽管不计算调用次数)。

What am I doing wrong? 我究竟做错了什么? This is my first time using Moq for this and as far as I can tell I've implemented the count functionality correctly. 这是我第一次使用Moq,据我所知,我已正确实现了计数功能。

EDIT: Following Chris Sinclair's suggestion, I've changed the initialiser to this, which fixed the issue: 编辑:按照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());

When you "Setup" the method, you set a callback but you don't provide a return value. 当您“设置”该方法时,您设置回调但不提供返回值。 As such, when the mocked method is called, it will return the default value for the return type (in this case, a Task<> type will result in a null return value). 因此,当调用mocked方法时,它将返回返回类型的默认值(在这种情况下, Task<>类型将返回null返回值)。 As such, when your Search method calls your mocked GetSearchInfoAsync method, it receives a null reference which naturally fails when it later attempts to invoke .ContinueWith on it. 因此,当你的Search方法调用你的嘲笑GetSearchInfoAsync方法,它收到一个null引用时,它后来试图调用这自然无法.ContinueWith就可以了。

Try adding a .Returns() which feeds a dummy Task<> to your mocked method: 尝试添加.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++);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM