C#单元测试问题

时间:2010-12-12 00:38:34

标签: c# unit-testing

如果使用xunit

,可以使用以下代码(实际上是想法)
public class RepositoryTester {

   private IRepository repository;

   public RepositoryTester(IRepository repository) {
      this.repository = repository;
   )

   [Fact] // Analogue of [Test] in other test packages.
   void CanDoWhatever() {
      // Test code
   }
}

现在,如果我尝试运行所有单元测试,只要xunit尝试通过调用RepositoryTester创建对象new RepositoryTester()(它调用不带参数的构造函数),它就会失败

我想做的事情可以用这种方式表达:

var tester1 = new RepositoryTester(new SQLRepository(...));
var tester2 = new RepositoryTester(new InMemoryRepository(...));

tester1. RUN_ALL_TESTS();
tester2. RUN_ALL_TESTS();

有人知道是否可能出现以下行为?(我真的希望通过它的界面为每个可测试的存储库使用相同的测试包)。

谢谢

1 个答案:

答案 0 :(得分:2)

您可以使RepositoryTester抽象,并为每种类型的存储库创建一个派生类,该类在无参数构造函数中创建适当的IRepository。将为每个具体的子类运行继承的测试方法。