[英]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++);
這將返回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());
當您“設置”該方法時,您設置回調但不提供返回值。 因此,當調用mocked方法時,它將返回返回類型的默認值(在這種情況下, Task<>
類型將返回null
返回值)。 因此,當你的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++);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.