开发者

Is there any good way to switch different context between Unit Test and Release. in C#

开发者 https://www.devze.com 2023-03-08 23:38 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消