unit-testing - Xamarin.Forms - 对使用 Autofac 中的 ServiceLocator 的方法进行单元测试
问题描述
我正在尝试对我的一个方法进行单元测试,该方法MyViewModel
从BaseViewModel
. 有这样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
解决方案
推荐阅读
- java - 尝试读取和创建文件时会导致无限循环
- powershell - Powershell执行while循环脚本输出意外
- javascript - javascript (Vanilla) - How to prevent HTML form post after an alert is shown
- java - 使用“clientId:clientSecret@servername/oauth/token”卷曲到休息模板
- reactjs - 缺少加载器、Webpack 和 React
- c# - 如何处理 DBNull 值
- oracle - 存储过程的 SQL 注入
- tsql - 在任何过滤器为真的地方选择不同的
- javascript - Javascript / JSON如何访问数组的值
- r - 时间序列不绘制绘图类型