Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Xamarin Forms - Unit testing a method using ServiceLocator

$
0
0

I am trying to unit test a method in my MyViewModel which extends from BaseViewModel. The BaseViewModel has a method like this

protected bool DoSomething()
{
   if (something)
   {
      ServiceLocator.Current.GetInstance ...; //ERROR
      return true;
   }
   else
   {
      return false;
   }
}

DoSomething is called pretty much in every view model. The app works as expected but I am trying to unit test (NUnit) and every method that calls DoSomething is failing in unit tests

The error I get is

error CS0119: 'ServiceLocator' is a type, which is not valid in the given context


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>