【发布时间】:2015-12-10 13:26:41
【问题描述】:
我正在使用 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)
但是,如果我注释掉该行,测试运行良好(尽管不计算调用次数)。
我做错了什么?这是我第一次为此使用起订量,据我所知,我已经正确实现了计数功能。
编辑:按照 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());
【问题讨论】:
-
当你“设置”方法时,你设置了一个回调但你没有设置一个返回值。如果我不得不猜测,这将使该方法返回返回类型的默认值,看起来这将是某种
Task<>(因此默认值将是null)。您的Search方法调用模拟方法,该方法返回null,然后在该空引用上调用.ContinueWith()。也许尝试将.Returns()添加到您的设置中,从而创建一个虚拟Task<>?抱歉,我对此不是 100% 肯定的。 -
你是对的,解决了它!请将您的回复分解为答案,以便我将其标记为解决方案!谢谢!
标签: c# unit-testing mocking moq