Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit tests that inherit from a type that uses generics
    text
    copied!<p>I have an interface with generics that is implemented by some classes. For each of these classes there is proxy class that implement the interface as well. Giving roughly this code:</p> <pre><code>public interface ISomeInterface&lt;T&gt; { T SomeProperty { get; } T SomeAction(); } public interface IClassA : ISomeInterface&lt;string&gt; { void Action(); } public class ClassA : IClassA { // Code goes here } public class ClassAProxy : IClassA { // Code goes here } </code></pre> <p>The unit tests code I would want to look something like this:</p> <pre><code>public abstract class ISomeInterfaceTests&lt;T&gt; { [TestMethod()] public void SomePropertyTest() { ISomeInterface&lt;T&gt; target; ISomeInterface&lt;T&gt; oracle; this.CreateInstance(out target, out oracle); Assert.AreEqual(oracle.SomeProperty, target.SomeProperty); } [TestMethod()] public void SomeActionTest() { ISomeInterface&lt;T&gt; target; ISomeInterface&lt;T&gt; oracle; this.CreateInstance(out target, out oracle); T oracleValue = oracle.SomeAction(); T targetValue = target.SomeAction(); Assert.AreEqual(oracleValue, targetValue); } // More tests protected abstract void CreateInstance(out ISomeInterface&lt;T&gt; target, out ISomeInterface&lt;T&gt; oracle); } [TestClass()] public class ClassAProxyTests : ISomeInterfaceTests&lt;string&gt; { // ClassAProxy specific tests protected override void CreateInstance(out ISomeInterface&lt;string&gt; target, out ISomeInterface&lt;string&gt; oracle) { // Create target as ClassAProxy here and oracle as ClassA } } </code></pre> <p>But this gives the error: UTA002: TestClass attribute cannot be defined on generic class ISomeInterfaceTests&lt;T&gt;.</p> <p>Is there some nice workaround to this? Currently the best solution I can think of is to have a method in ClassAProxyTests that calls the different test methods in ISomeInterfaceTests&lt;T&gt;. There are several problems to that approach though:</p> <ul> <li>It has to be done manually for each test implementing ISomeInterfaceTests&lt;T&gt;.</li> <li>Should one method result in an assertion that fails then the remaining methods will not be executed.</li> <li>You cannot use the ExpectedException attribute and would have to wrap the required code in try catch statements.</li> </ul> <p>but alas a better solution escapes me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload