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:
public interface ISomeInterface<T>
{
T SomeProperty
{
get;
}
T SomeAction();
}
public interface IClassA : ISomeInterface<string>
{
void Action();
}
public class ClassA : IClassA
{
// Code goes here
}
public class ClassAProxy : IClassA
{
// Code goes here
}
The unit tests code I would want to look something like this:
public abstract class ISomeInterfaceTests<T>
{
[TestMethod()]
public void SomePropertyTest()
{
ISomeInterface<T> target;
ISomeInterface<T> oracle;
this.CreateInstance(out target, out oracle);
Assert.AreEqual(oracle.SomeProperty, target.SomeProperty);
}
[TestMethod()]
public void SomeActionTest()
{
ISomeInterface<T> target;
ISomeInterface<T> 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<T> target, out ISomeInterface<T> oracle);
}
[TestClass()]
public class ClassAProxyTests : ISomeInterfaceTests<string>
{
// ClassAProxy specific tests
protected override void CreateInstance(out ISomeInterface<string> target, out ISomeInterface<string> oracle)
{
// Create target as ClassAProxy here and oracle as ClassA
}
}
But this gives the error: UTA002: TestClass attribute cannot be defined on generic class ISomeInterfaceTests<T>.
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<T>. There are several problems to that approach though:
- It has to be done manually for each test implementing ISomeInterfaceTests<T>.
- Should one method result in an assertion that fails then the remaining methods will not be executed.
- You cannot use the ExpectedException attribute and would have to wrap the required code in try catch statements.
but alas a better solution escapes me.