mardi 19 septembre 2017

How to mock a class to be found via reflection?

I have a method that finds a class using reflection (simplified):

public static Type GetClass(string qualifiedName)
{
    // Some init...
    Type myType = Type.GetType(qualifiedName);
    // Some checks...
    return myType;
}

To unit test this method I need to "mock" test classes that the tested method should be able to find via reflection. I can't use real classes from the tested project for some of the tests, because I need to test that it throws exception in certain situations that shouldn't exist there. For example, a class that doesn't implement a specific interface should cause an exception to be thrown, but in the tested assembly no such classes exist that I can test with.

How can I achieve this?

If I hard code the test classes in the unit test project, the tested method can't find them. So how can I get classes from the unit test project to be visible from the tested method?

If NSubstitute (or other framework/tool) can help, I'd appreciate info about what and how.





Aucun commentaire:

Enregistrer un commentaire