I am really stuck on C# Moq. I know I must be doing something wrong. Basically I am trying to return a value from an asynchronous method that's buried in a line of asynchronous methods.
Code:
var mockBBClient = new Mock<IAppFxClient>();
mockBBClient.Setup(x => x.RequestAsync("ping", "ping", 1))
.Returns(Task.FromResult("sdsd"));
The mockBBClient is then passed around and winds up in the code below as Client.
The weird part is SO seems to suggest that the "sdsd" above is ignored. Task.FromResult just needs some value in there that matches the Task return type. So if that's true how the heck do I get a mock response to the methods that are calling RequestAsync in my code?
The method I'm mocking (RequestAsync) returns Task<string>. The compiler is fine with my code. But I only get a blank response in the method of code that's calling RequestAsync on the mock db access client.
So I broke down the response when running live vs. running in test mode. First I output the Task (promise I guess) before awaiting it, then after.
Code:
public async Task<string> GetData(InputsT inputs, DataListLoadOptions options)
{
var response = Client.RequestAsync("DataListLoad", "XYZ");
Console.WriteLine("pre await response");
Console.WriteLine(response);
var response2 = await response;
Console.WriteLine("post await response");
Console.WriteLine(response2);
Console.WriteLine("FINISH");
return response2;
}
For live results I get:
Code:
response
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.String,EA.Blackbaud.SoapClient+<RequestAsync>d__9]
response2
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
... buncha XML ...
</soap:Envelope>
FINISH
For mock results I get:
Code:
response
System.Threading.Tasks.Task`1[System.String]
response2
FINISH
Is it something weird because we have a task within a task?
Last edited by suzzer99; 03-27-2019 at 08:35 PM.