You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While mocking an object that have a synchronous method which internally executes an asynchronous method is not returning correct results. Eventhough, the setup was configured correctly, it didn't execute the nested asynchronous methods that returns a Task. Following is my object to mock.
publicclassServiceClient{privatereadonlyUribaseUri;publicServiceClient(stringbaseUri){this.baseUri =new Uri(baseUri);}publicvoidProcess(){
Console.WriteLine("Process has been started");
DoSomething();// this is not working as well..// DoSomething().Wait();
Console.WriteLine("Process has been completed");}publicvirtual Task DoSomething(){
Console.WriteLine("DoSomething has been started");return DoSomethingMore();}publicvirtual Task DoSomethingMore(){
Console.WriteLine("DoSomethingMore has been started");return Task.CompletedTask;}}
This test fails and verify confirms that DoSomethingMore() method was never executed.
Complete source code is also attached to easily reproduce the issue. MoqTesting.zip
The text was updated successfully, but these errors were encountered:
The problem here is that Moq also mocks the DoSomething method. Because you haven't provided a setup for DoSomething, it will just return the default empty value for Task, which is a completed task. Your logic in DoSomething will never run.
You can get Moq to call your logic in DoSomething by setting client.CallBase to true:
While mocking an object that have a synchronous method which internally executes an asynchronous method is not returning correct results. Eventhough, the setup was configured correctly, it didn't execute the nested asynchronous methods that returns a Task. Following is my object to mock.
And following is my unit test.
This test fails and verify confirms that DoSomethingMore() method was never executed.
Complete source code is also attached to easily reproduce the issue.
MoqTesting.zip
The text was updated successfully, but these errors were encountered: