首页 > 解决方案 > Xamarin.Forms - 对使用 Autofac 中的 ServiceLocator 的方法进行单元测试

问题描述

我正在尝试对我的一个方法进行单元测试,该方法MyViewModelBaseViewModel. 有这样BaseViewModel的方法

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

DoSomething在每个视图模型中都被调用。该应用程序按预期工作,但我正在尝试进行单元测试(NUnit)并且调用的每个方法DoSomething在单元测试中都失败了

我得到的错误是

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

ServiceLocator来自_Autofac.CommonServiceLocator

标签: unit-testingxamarin.formsnunitautofacservice-locator

解决方案


推荐阅读