I have mocked async function, but, whenever I am invoking mocked function, it is returning null value.
I have an async function as shown below
public async Task<TResult> InvokeAzureFunctionWithTokenGetResultAsync<TArg, TResult>(TArg arg, string functionUri, string resourceid, CancellationToken canceltoken)
I want to write xUnit test case, as I cannot execute actual function, I am trying to mock it up. when executed there is no error, but when the function is invoked, I am getting null.
Actual function
public async Task<TResult> InvokeAzureFunctionWithTokenGetResultAsync<TArg, TResult>(TArg arg, string functionUri, string resourceid, CancellationToken canceltoken)
{
// some external code here
if (message != null && (message.StatusCode == HttpStatusCode.OK || message.StatusCode == HttpStatusCode.Created))
{
try
{
return JsonConvert.DeserializeObject<TResult>(await message.Content.ReadAsStringAsync().ConfigureAwait(true));
}
catch (JsonReaderException re)
{
throw new FormatException(re.Message, re);
}
catch (FormatException fe)
{
throw new FormatException(fe.Message, fe);
}
}
else if (message != null)
{
throw new FormatException(message.ReasonPhrase);
}
return default(TResult);
}
Test function
IHttpEndpointInvoker httpEndpointInvoker = new HttpEndpointInvoker();
IHttpEndpointInvoker httpEndpointInvoker = new HttpEndpointInvoker();
try
{
var mock = new Mock<IHttpEndpointInvoker>();
var obj = (new RouteDefinition() { Id = routeDefinitionId, IoTHubId = "" });
mock.Setup(s => s.InvokeAzureFunctionWithTokenGetResultAsync<RouteDefinition, string>(It.IsIn<RouteDefinition>(), It.IsAny<string>(), It.IsAny<string>(), new CancellationTokenSource().Token)).Returns(Task.Factory.StartNew(() => GetHttpResponseMessage(HttpStatusCode.Accepted).Result));
var o1 = mock.Object.InvokeAzureFunctionWithTokenGetResultAsync<RouteDefinition, string>(obj, "functionUri", "resourceid", new CancellationTokenSource().Token).GetAwaiter().GetResult(); // this line is returning null. But I am expecting value that is returned by "GetHttpResponseMessage" function
}
catch (Exception ex)
{
throw;
}
Note: "GetHttpResponseMessage" is successfully called and returning a string. I have debugged it.
Please check the line with below comments given in the above code for expected output/results.
// this line is returning null. But I am expecting value that is returned by "GetHttpResponseMessage" function
Aucun commentaire:
Enregistrer un commentaire