如何测试 WCF 服务的客户端

发布于 2024-09-28 01:43:47 字数 749 浏览 2 评论 0原文

我有一个 WCF 服务,它公开了 1 个访问数据库的方法 GetNextObjectInList(int id)

WCF 服务的工作原理或多或少是这样的:

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

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

    /* Dispose releases DB connection */
}

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

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

    /* use object */
}

我编写了单元测试来测试 WCF 服务对象,但我想测试消费者代码的各种功能,例如计时/性能/错误处理,但我不知道如何构建我的测试,以便服务不会访问数据库。

我的大多数测试(例如针对服务对象运行的测试)都会创建内存数据库,但我不知道如何让服务连接到该数据库,而无需在服务中添加特定于测试的代码。

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

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 */
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2024-10-05 01:43:47

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

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

如果您使用的是 VS 测试项目,您可以使用ClassInitialize / ClassCleanup 属性来设置/拆除服务:

    [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)内部,您可以提供测试客户端所需的任何行为,而不必担心单元测试会破坏生产代码

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文