我已经用同样的场景做了很多次了,但这一次失败了,我想不出原因,请帮帮我.
下面是我的代码,我已经try 了Moq和NSubsitute,但对于这两个,返回是null
,但它应该返回json,我放在里面,我已经做了相同的在以前的项目,但从来没有遇到过这样的问题,我的头脑完全不工作.
public interface IMyInterface
{
public string myOrder(List<Order> order);
}
目标类
public class funcMyFunctionApp
{
private readonly IMyInterface myInterface;
private readonly TelemetryClient telemetryClient;
private readonly ILogger _logger;
public funcMyFunctionApp(IMyInterface _myInterface)
{
myInterface = _myInterface;
}
[FunctionName("func1")]
public IActionResult func1([HttpTrigger(AuthorizationLevel.Function, "post",Route = "func1")] string jsoninput)
{
try
{
if (!string.IsNullOrEmpty(jsoninput))
{
return new OkObjectResult(myInterface.myOrder(JsonConvert.DeserializeObject<List<Order>>(jsoninput)));
}
else
{
return new BadRequestObjectResult("Bad Order");
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
if(ex.InnerException != null)
{
_logger.LogError(ex.InnerException.Message.ToString());
}
return new BadRequestObjectResult("Bad Order");
}
}
}
测试fixture
public void shouldReturnOkObjectResultifInputisValid()
{
string ValidJson1 = @"[
{
""CreatedBy"":""test@test.org"",
""OrderNo"":""Pizz111"",
""Amount"":""10$"",
}
]";
string ValidOutputJson1 = @"{
""OrderNo"": ""808098"",
""Item"": ""Pizza""
}";
Mock<IMyInterface> myInterfaceMock =new Mock<IMyInterface>();
IMyInterface myInterfaceSubsititueMock = Substitute.For<IMyInterface>();
var input = JsonConvert.DeserializeObject<List<Order>>(ValidJson1);
myInterfaceMock.Setup(m=>m.myOrder(input)).Returns(ValidOutputJson1);
//Local Arrange
myInterfaceSubsititueMock.myOrder(ValidJson1).Returns(ValidOutputJson1);
funcMyFunctionApp funcRequest = new funcMyFunctionApp(myInterfaceMock.Object);
// Act
var result = funcRequest.func1(ValidJson1);
// Assert
Assert.Multiple(() =>
{
Assert.That((((OkObjectResult)result).Value as string), Is.EqualTo(ValidOutputJson1));
});
}
我期望它应该返回Returns
以下提供的字符串