简体   繁体   English

使用ServerManager .net C#的单元测试方法

[英]Unit testing method that uses ServerManager .net C#

How would you test such method that uses ServerManager. 您将如何测试使用ServerManager的这种方法。 Obviously you can't mock ServerManager. 显然,您无法模拟ServerManager。 And this method is void. 而且此方法无效。 Is there a workaround? 有解决方法吗? Should one event test such method? 一个事件应该测试这种方法吗? Thanks! 谢谢!

 public void AppPoolRemoval(string poolName)
    {
        using (var serverManager = new ServerManager())
        {
            var oldPool = serverManager.ApplicationPools[poolName];
            if (oldPool != null)
            {
                oldPool.Delete();
                serverManager.CommitChanges();
            }
        }
    }

There's no way to unit test this code in isolation. 无法隔离地对该代码进行单元测试。 You'd better write an integration test for it where you would have some IIS instance to test it on. 您最好为此编写一个集成测试,其中要有一些IIS实例对其进行测试。

You could abstract this logic behind an interface: 您可以在接口后面抽象此逻辑:

public interface IWebServer
{
    void AppPoolRemoval(string poolName);
}

and then unit test in isolation the code that depends on this. 然后对依赖于此的代码进行单元测试。 But the actual implementation which is tied to a real ServerManager cannot be unit tested. 但是绑定到真实ServerManager的实际实现无法进行单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM