我是 nSubstitute 的新手。我正在为我的控制器类编写测试方法。我有一个名为 GetDefaultStateTest() 的 TestMethod,它具有 Substitute 类,如下所示
[TestMethod]
public void GetDefaultStateTest()
{
var _GetDefaultState = Substitute.For<CustomerController>(ICustomer cus);
Assert.IsNotNull(_GetDefaultState.GetDefaultState());
}
因为我的控制器类具有如下的参数化构造函数。
public class CustomerController : Controller
{
private readonly ICustomer _customer;
public CustomerController(ICustomer customer)
{
_customer = customer;
}
public string GetDefaultState()
{
// Get default state from settings table
List<tblSettings> settings = new List<tblSettings>();
// Calling service method GetSettings
settings = _customer.GetSettings();
var defaultState = from setting in settings
where setting.Desc == "DefaultState"
select setting.Settings;
string strState = "";
foreach (var oState in defaultState)
{
strState = oState;
break;
}
return strState;
}
}
在运行测试方法时,它会引发空引用问题。因为参数 ICustomer 为空
var _GetDefaultState = Substitute.For<CustomerController>(ICustomer cus);
如何解决这个问题。