Looking for .NET mocking framework that allows testing methods having non-injected dependencies
I'm wondering if there's any mocking framework that allows testing methods contaning method calls to dependent class instances not injected to the tested method or its class:
void MethodToTest()
{
....
DependentClass dc = new DependentClass();
dc.Foo();
....
}
In the code above I would like to mock the call to Foo(). I guess it could be accompl开发者_开发知识库ished by a mocking framework using code injection? Thank you in advance for tips.
I have never used it but based on some articles Moles should be able to do that.
MSDN says:
Mole types use a powerful detouring framework that uses code profiler APIs to intercept calls to dependency classes and redirects the calls to a fake object
If you want to mock the dependent object, you'll need a way to provide a mock instance. The best way to do this is to use dependency injection. It will not only make your code cleaner but it will also help decouple your classes, improving your design.
精彩评论