我有一个名为 MvcContacts 的解决方案,其中有一个名为MvcContacts的 MVC 4 项目和一个名为MvcContacts.Test的单元测试项目(当我在 Visual Studio 中选中“启用单元测试”框时自动创建。
我将 Ninject.MVC NuGet 包安装到MvcContacts(MVC 项目)中。
我按照本教程进行了所有设置,没有错误,它应该可以正常工作;但是,我的单元测试项目MvcContacts.Test有这样的错误:
'MvcContacts.Controllers.HomeController' 不包含采用 0 个参数的构造函数
这是我的HomeController
:
namespace MvcContacts.Controllers
{
public class HomeController : Controller
{
Logging.ILogger _logger;
public HomeController(Logging.ILogger logger)
{
_logger = logger;
}
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
_logger.LogMessage(ViewBag.Message);
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
这是我的HomeControllerTest
:
namespace MvcContacts.Tests.Controllers
{
[TestClass]
public class HomeControllerTest
{
[TestMethod]
public void Index()
{
// Arrange
HomeController controller = new HomeController(); // error on this line
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
Assert.AreEqual("Modify this template to jump-start your ASP.NET MVC application.", result.ViewBag.Message);
}
[TestMethod]
public void About()
{
// Arrange
HomeController controller = new HomeController(); // error on this line
// Act
ViewResult result = controller.About() as ViewResult;
// Assert
Assert.IsNotNull(result);
}
[TestMethod]
public void Contact()
{
// Arrange
HomeController controller = new HomeController(); // error on this line
// Act
ViewResult result = controller.Contact() as ViewResult;
// Assert
Assert.IsNotNull(result);
}
}
}
如何使我的单元测试项目与 Ninject 一起工作?