开发者

C# unit testing question

Does someone if the following code (idea, actually) is possible using 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
   }
}

Now, if I attempt to run all uni开发者_如何转开发t test, it would fail as long as xunit attempts to create the object RepositoryTester by calling new RepositoryTester() (it invokes the constructor without parameters).

What I want to do can be equivalently expressed this way:

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

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

Does someone know if the following behavior is possible? (I really wish to use the same test package for every testable repository through it's interface).

Thank you


You can make RepositoryTester abstract, and have a derived class for each type of repository that creates an appropriate IRepository in a parameter-less constructor. The inherited test methods will be run for each concrete child class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜