如何测试WCF服务的客户端 [英] How to test a client of a WCF service

查看:80
本文介绍了如何测试WCF服务的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务,该服务公开了1种方法GetNextObjectInList(int id),该方法命中了数据库.

I have a WCF service that exposes 1 method GetNextObjectInList(int id) which hits a DB.

WCF服务或多或少像这样运行:

The WCF service works, more or less, like this:

public class MyService : IDisposable
{
    public MyService() 
    { 
        this.IntializeDBConnection();
    }

    public int GetNextObjectInList(int id) 
    { 
        /* uses DB connection */ 
    }

    /* Dispose releases DB connection */
}

这使客户端代码相对简单:

This makes the client code relatively simple:

public void UseNextElementInList()
{
    IMyService svc = new MyServiceClient();
    int nextID = svc.GetNextObjectInList(this.ID);

    /* use object */
}

我已经编写了单元测试来测试WCF服务对象,但是我想测试使用者代码中的诸如计时/性能/错误处理之类的各种事情,但是我不知道如何构造测试以使WCF服务对象能够对WCF服务对象进行测试.服务没有打到数据库.

I've written unit tests to test the WCF services objects, but I'd like to test the consumer code for various things like timing/performance/error handling but I don't know how to construct my tests such that the Service doesn't hit the DB.

我的大多数测试(例如针对服务对象运行的测试)确实会创建一个内存数据库,但是我不知道如何在没有服务中特定于测试的代码的情况下将服务连接到该数据库.

Most of my tests (the tests that run against the service's objects for instance) DO create an in-memory DB but I don't know how to get the service to connect to that without test-specific code in the service.

推荐答案

我将为您的单元测试创​​建一个测试服务.通常,在这种情况下,我要做的是为测试项目创建一个与真实项目相同的配置,除了地址是本地主机,而类型是我的测试服务类:

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"/>

如果使用的是VS Test Project,则可以使用ClassInitialize/ClassCleanup属性来设置/拆除服务:

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;
    }

现在,在TestService类(将实现IMyService)内部,您可以提供测试客户端所需的任何行为,而不必担心单元测试会破坏您的生产代码

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

这篇关于如何测试WCF服务的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆