我已经在mvc项目中实现了ninject,现在按预期工作了,我想要进行单元测试,但似乎无法让它工作:
控制器:
private IDependencyResolver _resolver;
public HomeController(IDependencyResolver resolver)
{
_resolver = resolver;
}
public ActionResult Index()
{
var model = _resolver.GetService<ISignUpViewModel>();
return PartialView("Login", model);
}
单元测试
private IDependencyResolver _resolverMock;
[TestMethod]
public void SignUpTest()
{
var ctrl = new HomeController(_resolverMock);
var signUpMock = new Mock<ISignUpViewModel>();
ctrl.Index();
ctrl.ViewData.Model = signUpMock;
//Assert.AreEqual("", "");
}
解析器始终为空。
我看过Ninject.MockingKernel.Moq我应该在那里设置解析器吗?
知道我哪里出错了?
由于
答案 0 :(得分:0)
在单元测试中,您手动创建HomeController
实例,因此您需要模拟IDependencyResolver
。例如:
var resolverMock = new Mock<IDependencyResolver>();
var ctrl = new HomeController(resolverMock.Object);
....