【发布时间】:2019-04-11 07:57:11
【问题描述】:
我正在开发使用 ASP.NET Core 2.1 的 Web 应用程序项目。除了开发 API,我们还尝试使用 MSTest 框架对其进行单元测试。
我的控制器继承自 ControllerBase。在我的测试台中,我正在使用 Moq 框架模拟我的业务层。当我从测试方法调用控制器时,我需要将一个模拟业务实例传递给控制器,我正在尝试为其声明参数化构造函数。
测试用例运行良好,但我的正常流程受到干扰。我什至尝试使用参数化和无参数构造函数。
这适用于继承 APIController 的 Dot Framework。
public class BookingController: ControllerBase {
BusinessManager business = new BusinessManager();
//Non-Parameterized Constructor
public BookingController() {}
//Parameterized Constructor
public BookingController(BusinessManager mockedBusiness) {
this.business = mockedBusiness;
}
}
从 UI 调用时应使用非参数化构造函数。 参数化应该只在从测试台调用传递一些实例时才起作用。
【问题讨论】:
标签: c# unit-testing asp.net-core asp.net-core-2.0 mstest