Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 759
Yippie-Ki-Yay Avatar asked Jul 02 '26 15:07

Yippie-Ki-Yay


1 Answers

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.

like image 196
Adam Vandenberg Avatar answered Jul 04 '26 05:07

Adam Vandenberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!