michielpost.nl

Windows 8 async programming and mocking async methods in MVVM

11-07-2012

Consider you created a service interface with a method which has a return type of Task:

public interface IMyService
{
  Task<string> GetStringAsync();
}

When you inplement this interface, you're probably doing some async calls in your method:

public class MyService : IMyService
{
  public async Task<string> GetStringAsync()
  {
     string result = await DoAsyncWebRequestEtc();
 
     return string;
  }
}

The method returns a Task, but because the method is marked async, you can just return a string.
See MSDN (Async Return Types) for more info http://msdn.microsoft.com/en-us/library/hh524395(v=vs.110).aspx

But when you're creating a mock for this service, you're not going to do any async calls. So you don't need the async, but then you have to return an actual Task. That's not what we want.

If we keep the async and just return a string, we will get a compiler warning about a lacking await operator:

public class MyServiceMock : IMyService
{
  public async Task<string> GetStringAsync()
  {
     return "mock string";
  }
}

Warning from Visual Studio:
This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

We can fix this by dropping the async and return an actual task by using the helper Task.FromResult.

public class MyServiceMock : IMyService
{
  public Task<string> GetStringAsync()
  {
     return Task.FromResult("mock string");
  }
}

The compiler is happy and we keep our blendability with MVVM Light. (Value shows up in the UI when using MVVM Light).

 

Comments


Mattias 17-02-2013
Super! But what about the other case when there is no return value: Task DoWorkAsync(); You cannot call FromResult() with no arguments... :/


???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. https://helloworld.com?h=9928878115f3fdeea2b21fa74a5b24dc& 19-11-2020
???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. https://helloworld.com?h=9928878115f3fdeea2b21fa74a5b24dc&


???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. http://bit.do/fLc9r?h=9928878115f3fdeea2b21fa74a5b24dc& 19-11-2020
???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. http://bit.do/fLc9r?h=9928878115f3fdeea2b21fa74a5b24dc&


???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. https://helloworld.com?h=9928878115f3fdeea2b21fa74a5b24dc& 19-11-2020
???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. https://helloworld.com?h=9928878115f3fdeea2b21fa74a5b24dc&


???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. http://bit.do/fLc9r?h=9928878115f3fdeea2b21fa74a5b24dc& 20-11-2020
???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. http://bit.do/fLc9r?h=9928878115f3fdeea2b21fa74a5b24dc&


???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. http://bit.do/fLc9r?h=9928878115f3fdeea2b21fa74a5b24dc& 20-11-2020
???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. http://bit.do/fLc9r?h=9928878115f3fdeea2b21fa74a5b24dc&


???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. http://bit.do/fLc9r?h=9928878115f3fdeea2b21fa74a5b24dc& 21-11-2020
???????? ?? ? ????, ????? ???????? ??????, ? ? ????, ????? ???? ????? ????? ?????. http://bit.do/fLc9r?h=9928878115f3fdeea2b21fa74a5b24dc&


Buy legal anabolic steroids https://myfsk.org/community/account/buy-legal-anabolic-steroids/?h=9928878115f3fdeea2b21fa74a5b24dc& 13-12-2020
Buy legal anabolic steroids https://myfsk.org/community/account/buy-legal-anabolic-steroids/?h=9928878115f3fdeea2b21fa74a5b24dc&


<p><a href="https://f3gxp.page.link/Tbeh"><img src="https://i.ibb.co/rH6Ky7F/best-hookup-sites-fs.jpg"></a></p>?h=9928878115f3fdeea2b21fa74a5b24dc& 20-12-2020
<p><a href="https://f3gxp.page.link/Tbeh"><img src="https://i.ibb.co/rH6Ky7F/best-hookup-sites-fs.jpg"></a></p>?h=9928878115f3fdeea2b21fa74a5b24dc&


<p><a href="https://f3gxp.page.link/Tbeh"><img src="https://i.ibb.co/rH6Ky7F/best-hookup-sites-fs.jpg"></a></p>?h=9928878115f3fdeea2b21fa74a5b24dc& 21-12-2020
<p><a href="https://f3gxp.page.link/Tbeh"><img src="https://i.ibb.co/rH6Ky7F/best-hookup-sites-fs.jpg"></a></p>?h=9928878115f3fdeea2b21fa74a5b24dc&


IwoWC 10-01-2021
Medicament information sheet. What side effects can this medication cause? <a href="https://viagra4u.top">can you buy cheap viagra no prescription</a> in USA. Actual what you want to know about drugs. Read information here. <a href=https://251901.net/biyoujisyo/hasegawatakashi/comment-page-1461/#comment-1816646>Best trends of medicine.</a> <a href=http://satkesmabespolri.org/berita/detail/63/RUMAHOKSIGENPOLDARIAU>Actual trends of medication.</a> <a href=http://culturia.nl/node/17#comment-4670>Actual what you want to know about drug.</a> 3a338_3


New Comment

Name
E-mail (not published)
Comment
Enter the code shown: