Har*_*a W 2 c# asp.net-mvc unit-testing parameterized-unit-test
我已经创建了一个单元测试项目。我得到一个异常指定
无法获取类*****。Tests.Controllers.PersonRegistration的默认构造函数
namespace *****.Tests.Controllers
{
[TestClass]
public class PersonRegistration
{
private ILoggingService _loggingService;
private IUserManager _userManager;
public PersonRegistration(IUserManager userManager, ILoggingService loggingService)
{
this._userManager = userManager;
this._loggingService = loggingService;
}
[TestMethod]
public void TestMethod1()
{
RegisterBindingModel model = new RegisterBindingModel();
AccountController ac = new AccountController(_userManager, _loggingService);
model.UserName = "test123@gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了消除此问题,一些线程表示添加了不带参数的默认构造函数。所以我也加了
public PersonRegistration()
{
}
Run Code Online (Sandbox Code Playgroud)
但后来我解决了异常。但是我正在NULL为_userManager和获取价值
_loggingService
如何解决该问题。我不想在传递时生成空值。
请提出一种无需使用Moq或没有其他模拟框架的方法来解决此问题,以帮助我。
正如所讨论的。
测试类初始化后运行一次。
[ClassInitialize]
public void SetUp()
{
_loggingService = new LoggingService();
_userManager = new UserManager();
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用
[TestInitialize]
public void Initalize()
{
_loggingService = new LoggingService();
_userManager = new UserManager();
}
Run Code Online (Sandbox Code Playgroud)
如果您需要针对每个测试的不同条件,它将在运行每个测试之前运行。
正如所讨论的。ILoggingService和IUserManager是接口。您需要将它们实例化到该接口的任何实现类“实现”。
If you are unsure.. right click on the interface name and choose "Go To Implementation"
| 归档时间: |
|
| 查看次数: |
5721 次 |
| 最近记录: |