2
我有一個接口定義,其中,該方法具有定義如何獲得起訂量來驗證具有out參數
public interface IRestCommunicationService
{
TResult PerformPost<TResult, TData>(string url, TData dataToSend, out StandardErrorResult errors);
}
我有一個使用上述接口
public TripCreateDispatchService(IRestCommunicationAuthService restCommunicationService, ISettings settings)
{
_restCommunicationService = restCommunicationService;
_settings = settings;
}
public FlightAlert CreateTrip(string consumerNumber, PostAlertModel tripModel, out StandardErrorResult apiErrors)
{
url = .. code ommited
var result = _restCommunicationService.PerformPost<FlightAlert, PostAlertModel>(url), tripModel, out apiErrors);
return result;
}
以下類的輸出參數的方法
在我的單元測試中,我試圖驗證ResCommunication對象的PerformPost方法被調用。
但無論我做什麼,我不能讓起訂量,以驗證該方法被調用
public void DispatchService_PerformPost()
{
var consumerNumber = ...
var trip = ...
var result = ...
var apiErrors = new StandardErrorResult();
... code to setup mock data
_mockRestCommunicationService = new Mock<IRestCommunicationAuthService>();
_mockEestCommunicationService.Setup(x => x.PerformPost<string, PostAlertModel>(It.IsAny<string>(), It.IsAny<PostAlertModel>(), out apiErrors)).Verifiable();
_systemUnderTest.CreateTrip(consumerNumber, trip, out apiErrors);
_mockRestCommunicationService.Verify(m =>
m.PerformPost<StandardErrorResult, PostAlertModel>(
It.IsAny<string>(),
It.IsAny<PostAlertModel>(), out apiErrors
), Times.Once);
}
但我收到以下錯誤
Moq.MockException :
Expected invocation on the mock once, but was 0 times:
m => m.PerformPost<StandardErrorResult,PostAlertModel>(It.IsAny<String>(), It.IsAny<PostAlertModel>(), .apiErrors)
No setups configured.
如何去驗證該方法被調用。
我使用起訂量和NUnit
UPDATE 1
由於每從陽光的評論,我已經修改了測試如下使用回調
var consumerNumber = ...
var trip = ...
var result = ...
StandardErrorResult apiErrors;
_mockRestCommunicationService.Setup(x => x.PerformPost<string, PostAlertModel>(
It.IsAny<string>(), It.IsAny<PostAlertModel>(), out apiErrors))
.Callback<string, PostAlertModel, StandardErrorResult>
((s, m, e) => e.Errors = new System.Collections.Generic.List<StandardError>()
{
new StandardError { ErrorCode = "Code", ErrorMessage = "Message" }
});
_systemUnderTest.CreateTrip(consumerNumber, trip, out apiErrors);
Assert.That(apiErrors.Errors, Is.Not.Null);
這是執行測試時正在拋出的錯誤。
System.ArgumentException : Invalid callback. Setup on method with
parameters (String,PostAlertModel,StandardErrorResult&)
cannot invoke callback with parameters (String,PostAlertModel,StandardErrorResult).
at Moq.MethodCall.ThrowParameterMismatch(ParameterInfo[] expected, ParameterInfo[] actual)
at Moq.MethodCall.SetCallbackWithArguments(Delegate callback)
at Moq.MethodCallReturn`2.Callback(Action`3 callback)
此錯誤在安裝程序語句中引發。
僅供參考。我正在使用Resharper 8並使用他們的測試運行器來執行我的測試。
如果我嘗試將out參數添加到回調中,代碼將無法編譯。
我得到同樣的錯誤,如果我修改設置,以
_mockRestCommunicationService.Setup(x => x.PerformPost<string, PostAlertModel>(
It.IsAny<string>(), It.IsAny<PostAlertModel>(), out apiErrors))
.Callback
((string s, PostAlertModel m, StandardErrorResult e) => e.Errors = new System.Collections.Generic.List<StandardError>()
{
new StandardError { ErrorCode = "Code", ErrorMessage = "Message" }
});
我嘗試使用回調,現在我收到以下錯誤消息:無效的回調。帶有參數的方法的設置不能用參數調用回調。 我也嘗試過使用泛型方法調用。 回調(s,m,e) –
您可以發佈確切的異常消息。如果應該包含什麼是預期的參數,以及你設置了什麼。 –