我需要单元测试构造函数吗?
我的项目设置如下:
- 数据 - 存储库。
- 服务
- Web
我正在使用 NUnit,并且正在测试 Service
项目中的方法。当我第一次使用 MSTest 自动为我设置这些测试时,它创建了一个单元测试构造函数,如下所示:
Service service;
[Test]
public void ServiceConstructorTest()
{
IRepository repository = null; // TODO: Initialize to an appropriate value
service = new Service(repository );
Assert.Inconclusive("TODO: Implement code to verify target");
}
当我尝试测试方法时,此构造函数不会执行,并且 service
最终为 null 。每次编写测试时都必须声明和模拟吗?
I have projects set up as follows:
- Data - repository.
- Service
- Web
I'm using NUnit, and I'm testing a method that is in Service
Project. When I first used MSTest to automatically setup these tests for me, it created a unit test constructor that looks like this:
Service service;
[Test]
public void ServiceConstructorTest()
{
IRepository repository = null; // TODO: Initialize to an appropriate value
service = new Service(repository );
Assert.Inconclusive("TODO: Implement code to verify target");
}
When I try to test a method, this constructor is not executed and service
ends up being null. Am I going to have to declare and mock every time I write a test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NUnit 有一个 [SetUp] 属性,您可以将其放在方法上,并且该方法将在执行每个测试之前调用。还有一个 [TearDown] 属性,该属性也会导致该方法在每个单元测试之后运行。
NUnit has a [SetUp] attribute that you put on a method, and that method will be called before each test is executed. There is also a [TearDown] attribute that causes the method to be run after each unit test as well.