I have some context in code to be switched depending on whether it is running under test or release. Say in my prod开发者_如何转开发uct coding:
PublishRequest(); // the real one
//PublishRequestPsudo(); // the one want to be run during unit test
The way I am thinking about is make a TestFlag class:
if (!TestFlag.PublishFlag)
{
PublishRequest();
}
else
{
PublishRequestPsudo();
}
This seems verbose if I have many place to do that. Is there any good pattern to do it?
A pretty good way to acheive the same is Dependency Injection/Inversion Of Control
Another good resource on this is Caching ArchitectureTestability, Dependency Injection and Multiple Providers
精彩评论