【发布时间】:2014-05-02 13:23:23
【问题描述】:
我正在做单元测试。我有一个依赖于多个存储库的控制器(意味着有多个依赖项)。
下面是控制器的一段代码:-
public class TestController : BaseController
{
//Declaring dependencies
private IProductService _productService;
private IStudenctService _studentService;
private ITeacherService _teacherService;
private IClassService _classService;
private ITaxService _taxService;
private ICategoryService _categoryService;
private ISchoolService _schoolService;
//Constructor calling…
public TestController ()
{
}
public TestController (IProductService productService)
{
this._ productService = productService;
}
public TestController (IStudenctService studenctService)
{
this._ studentService = studenctService;
}
public TestController (ITeacherService teacherService)
{
this._ teacherService = teacherService;
}
public TestController (IClassService classService)
{
this._ classService = classService;
}
public TestController (ITaxService taxService)
{
this._ taxService = taxService;
}
public TestController (ICategoryService categoryService)
{
this._ categoryService = categoryService;
}
public TestController (ISchoolService schoolService)
{
this._ schoolService = schoolService;
}
public TestController (ISchoolService schoolService, ICategoryService categoryService)
{
this._ schoolService = schoolService;
this._ categoryService = categoryService;
}
public TestController (ISchoolService schoolService, ICategoryService categoryService, ITaxService taxService, IClassService classService) {
this._ schoolService = schoolService;
this._ categoryService = categoryService;
this._ taxService = taxService;
this._ classService = classService;
}
}
在上面的代码中,我为每个依赖项创建了单独的构造函数。其中有 2 个构造函数采用多个参数(依赖项 arg)。
通过这种方式,我的单元测试方法运行良好。但是当我运行应用程序时,它给了我以下错误:-
呃-哦,出了点问题!错误代码:500
但是当我只调用一个参数构造函数时,它就可以工作了。谁能告诉我这里出了什么问题?多依赖怎么办?
【问题讨论】:
-
错误代码500与Unity无关我认为错误不是来自depeandecy注入错误应该在其他地方
-
配置 Visual Studio 以中断所有异常(通过转到 Debug/Exceptions... 并选中“Common Language Runtime Exceptions”行的“Thrown”和“User-unhandled”复选框)看看弹出了什么确切的异常。始终在您的问题中发布异常详细信息。
标签: c# unit-testing dependency-injection unity-container