How to test a client of a WCF service

烂漫一生 提交于 2019-12-06 16:52:39

I would create a test service for your unit tests. Typically what I do in these circumstances is create a config for the test project that is identical to the real one except the address would be local host, and the type would be my test service class:

        <service name="MyNamespace.TestService" behaviorConfiguration="BehaviorConfig">
            <endpoint address="net.tcp://localhost/MySolution/TestService"
                                binding="netTcpBinding"
                                bindingConfiguration="BindingConfig"
                                contract="MyNamespace.IMyService"/>

If you are using VS Test Project you can use the ClassInitialize / ClassCleanup attributes to set up / tear down the service:

    [ClassInitialize()]
    public static void MyClassInitialize(TestContext testContext) {
        mHost = new ServiceHost(typeof(TestService));
        mHost.Open();
        return;
    }
    [ClassCleanup()]
    public static void MyClassCleanup() {
        if(mHost != null) {
            mHost.Close();
        }
        return;
    }

Now inside of the TestService class (which would implement IMyService) you could provide whatever behavior necessary to test the client without worrying about your unit tests corrupting your production code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!