Lou*_*hys 2 tdd unit-testing mocking
在使用mock/fake对象进行单元测试时,我想知道在SetUp或测试方法本身中初始化模拟是否更为可取,如果我的测试类包含测试同一类的方法(因此,所需的假对象应该是对于所有测试案例都是一样的).像这样的东西:
class FooTests
{
Mock<IBar> mockBar;
Foo fooUnderTest;
[SetUp]
public void Setup()
{
mockBar = new Mock<IBar>();
fooUnderTest = new fooUnderTest(mockBar.Object);
}
[Test]
public void MyTest()
{
//setup mock methods
mockBar.SetUp( ... )
//test
fooUnderTest.TestSomething()
//assert something here
}
}
Run Code Online (Sandbox Code Playgroud)
看起来这会阻止我们在每个测试用例中将代码复制到mockBar和fooUnderTest,但缺点是我必须声明类变量mockBar和fooUnderTest(或者这真的是一个缺点?),如果我有问题想要有一些特殊的设置(例如,如果在某个测试用例中我想要覆盖Foo的一些虚拟方法).有什么建议什么是最佳做法?
| 归档时间: |
|
| 查看次数: |
1723 次 |
| 最近记录: |