3

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 unit 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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.