I have the following problem. I want to test a generic class MyClass<'T>. However, I don't really care about the type being used for T. However, when I create an instance of MyClass in the test, I need to specify what T is supposed to be. I can do it like that:
var sut = new MyClass<Object>();
It works of course, however, I don't like to put Object specifically, because it suggests to someone reading this test that maybe Object type is somehow significant here.
I like the approach used by AutoFixture where each value is created using fixture.Create<T>()
, because it shows the reader that the value is not really important. Example of that:
[Fact]
public void IntroductoryTest()
{
// Arrange
Fixture fixture = new Fixture();
int expectedNumber = fixture.Create<int>();
MyClass sut = fixture.Create<MyClass>();
// Act
int result = sut.Echo(expectedNumber);
// Assert
Assert.Equal(expectedNumber, result);
}
In the code above we don't care about the number, so we use AutoFixture as an abstraction about the fact that some number is needed.
Is there a solution like that, but in regard to types being used with generic classes?
Something like: var sut = MyClass<fixture.GenericType>();
I don't care here what is the actual type. To explain my use-case a bit more: I have AutoFac module that is used to register MyClass. In my test I just want to test if the module registers MyClass properly. For that registration I don't want to specify some exact type to be used for MyClass. It can be any.