C# 单元测试问题
是否有人使用C# 单元测试问题,c#,unit-testing,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
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
通过调用new RepositoryTester()
尝试创建对象RepositoryTester
(它调用构造函数而不带参数),它就会失败
我想做的事情可以等效地表示为:
var tester1 = new RepositoryTester(new SQLRepository(...));
var tester2 = new RepositoryTester(new InMemoryRepository(...));
tester1. RUN_ALL_TESTS();
tester2. RUN_ALL_TESTS();
有人知道以下行为是否可能吗?(我真的希望通过接口为每个可测试存储库使用相同的测试包)
谢谢您可以将
RepositoryTester
抽象化,并为每种类型的存储库创建一个派生类,在无参数构造函数中创建一个适当的IRepository。继承的测试方法将针对每个具体的子类运行