-1
我需要模擬一個方法以及它的返回值,它是一個對象。下面是我在主類嘲笑+ C#中捕獲方法的返回值問題#
//**Main Class**
var message = CorrelationMessageClient.GetCorrelationMessage(
orderRequest.OrderId, CorrelationMessageType.CorrelationFix);
if (message != null && !string.IsNullOrEmpty(message.CorrelationId))
{
sendOrderResponse = PrepareExecutionReport(
new MessageManager(message.ResponseMessage));
}
CorrelationMessageClient.GetCorrelationMessage的返回類型爲類型CorrelationMessage對象的代碼。
在我的單元測試我已經嘲笑如下
//**Unit Test class**
private Mock<ICorrelationMessageClient> _mockCorrelationMessageClient;
_mockCorrelationMessageClient = new Mock<ICorrelationMessageClient>();
var correlationResponse = new CorrelationMessage {
CorrelationId = "1212121",ResponseMessage = "Response Successful"};
_mockCorrelationMessageClient.Setup(x =>
x.GetCorrelationMessage(correlationMessageFix.CorrelationId,
correlationMessageFix.CorrelationMessageType))
.Returns(correlationResponse);
當調試主類代碼,我看到被傳遞給GetCorrelationMessage方法的值,但該消息變量保持爲空。爲什麼當我在下面的設置方法中寫回Return語句時,它爲空
_mockCorrelationMessageClient.Setup(x =>
x.GetCorrelationMessage(correlationMessageFix.CorrelationId,
correlationMessageFix.CorrelationMessageType))
.Returns(correlationResponse);