How can I mock HttpRequestMessage
, specifically the CreateResponse
?
var requestMessage = Substitute.For<HttpRequestMessage>();
requestMessage.CreateResponse().ReturnsForAnyArgs(
new HttpResponseMessage(HttpStatusCode.OK));
but I get the exception ...
NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException:
'Could not find a call to return from.
I've seen the questions ... How to mock the CreateResponse<T> extension method on HttpRequestMessage
And associated ... ASP.NET WebApi unit testing with Request.CreateResponse ...
But they don't seem to actually end up mocking the CreateResponse
Additional comments:
I'm trying to write a unit test around the starter of an Azure precompiled C# function ...
[FunctionName("Version")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
HttpRequestMessage req,
TraceWriter log)
{
log.Info("Version function processed a request ... ");
return req.CreateResponse(HttpStatusCode.OK, "Version 0.0.1");
}
and the actual test, where I want to mock up the HttpRequestMessage, specifically the CreateReponse where I get the error is ...
[TestMethod]
public void Version_returns_value()
{
var requestMessage = Substitute.For<HttpRequestMessage>();
requestMessage.CreateResponse(Arg.Any<HttpStatusCode>(), Arg.Any<string>())
.Returns(new HttpResponseMessage(HttpStatusCode.OK));
var log = new CustomTraceWriter(TraceLevel.Verbose);
var httpResponseMessage = VersionFunction.Run(requestMessage, log);
var httpContent = httpResponseMessage.Content;
httpContent.Should().Be("Version 0.0.1 :: valid");
}