Parameterized Unit Testing Class Types
I have the following test in xUnit:
I want to parameterize the test by giving classes (like ServiceEventsModel) instead of writing each one manually. so far, I've tried this:
but gives an error making it typeof(model) doesn't work and creates more errors.
2 Replies
You cannot use a Type object as a type parameter.
You need to use reflection, to create the correct method at runtime and invoke it.
https://learn.microsoft.com/en-us/dotnet/api/system.reflection.methodinfo.makegenericmethod?view=net-8.0
MethodInfo.MakeGenericMethod(Type[]) Method (System.Reflection)
Substitutes the elements of an array of types for the type parameters of the current generic method definition, and returns a MethodInfo object representing the resulting constructed method.
👍