C# 测试 WCF Web 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15154735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:17:15  来源:igfitidea点击:

Testing a WCF web service?

c#visual-studio-2010wcfweb-services

提问by w00

I wanted to create a test class for a WCF service. I believe "mocking" is the right term for this?

我想为 WCF 服务创建一个测试类。我相信“嘲笑”是正确的术语吗?

I'm not really sure that the way i think i have to do this is the correct way. I have been given a URL to a WCF service, for example:

我不确定我认为我必须这样做的方式是正确的方式。我得到了一个 WCF 服务的 URL,例如:

http:://somesite.com/wcf/RealService.svc

And:

和:

http:://somesite.com/wcf/RealService.svc?wsdl

So instead of actually adding the RealService.svcto my project as Service Referencei simply added a new empty WCF Serviceto my project called Service1.

因此RealService.svc,我没有将 实际添加到我的项目中,因为Service Reference我只是在我的项目中添加了一个WCF Service名为Service1.

I then want to use the wsdl.exe(or maybe the svcutil.exe?) tool to generate an interface from the WSDL url: http:://somesite.com/wcf/RealService.svc?wsdl.

然后我想使用wsdl.exe(或者可能是 svcutil.exe?)工具从 WSDL url 生成一个接口:http:://somesite.com/wcf/RealService.svc?wsdl

I then open the Service1.csfile and instead of letting is inherit from IService1.csi let it inherit from the generated interface.

然后我打开Service1.cs文件,而不是让它继承IService1.cs自我让它从生成的interface.

Then instead of calling the real service in my application i simply call my Service1class. Is that how mocking a web service works..?

然后我没有在我的应用程序中调用真正的服务,而是简单地调用我的Service1类。这就是模拟 Web 服务的工作原理..?

Also need to figure out how to actually generate an interfacewith the svcutil tool (i've read that i can't use wsdl.exe for a WCF service?). So any tips on that are more than welcome aswell!

还需要弄清楚如何interface使用 svcutil 工具实际生成一个(我已经读过我不能将 wsdl.exe 用于 WCF 服务?)。所以任何关于这方面的提示都非常受欢迎!

采纳答案by Channs

Many areas to touch upon, will attempt to point you in the right directions:

许多要触及的领域将尝试为您指明正确的方向:

  • If you want to test (i.e. pass input, verify output) your WCF service, use the Visual Studio GUI tool WCF Test Client(MSDN article here).

  • If you want to mock your WCF service (i.e. unit test your component which consumes the WCF service), use a mocking framework like NMock2which allows you to mock the Service Interface (related SO thread here). You could hand-code a mock too (by implementing the interface), if you don't want to use an external framework - but this is more involved.

  • If you want to unit test your WCF service (i.e. write unit tests for service, business, data, etc.), use a popular mocking framework (related SO thread here).

  • To generate a proxy for your WCF service, use the svcutil.exe command line utility (MSDN article here) as you guessed. This utility comes with various options (language, namespace, config file, etc.), so pay attention to them.

  • 如果要测试(即传递输入、验证输出)您的 WCF 服务,请使用 Visual Studio GUI 工具WCF Test Client此处为MSDN 文章)。

  • 如果您想模拟您的 WCF 服务(即单元测试您使用 WCF 服务的组件),请使用类似的NMock2模拟框架,该框架允许您模拟服务接口(此处为相关 SO 线程)。如果您不想使用外部框架,您也可以手动编写模拟代码(通过实现接口) - 但这更复杂。

  • 如果要对 WCF 服务进行单元测试(即为服务、业务、数据等编写单元测试),请使用流行的模拟框架(此处为相关 SO 线程)。

  • 要为您的 WCF 服务生成代理,请使用您猜到的 svcutil.exe 命令行实用程序(此处为MSDN 文章)。此实用程序带有各种选项(语言、命名空间、配置文件等),因此请注意它们。

Hope this helps.

希望这可以帮助。

回答by polkduran

You can generate you proxy using the svcutil.exe(from Visula Studio: Add a Service Reference...). This will generate your client proxy and a Service Interface.

您可以使用svcutil.exe生成代理(来自 Visula Studio:添加服务引用...)。这将生成您的客户端代理和服务接口

For exemple for your service http:://somesite.com/wcf/RealService.svc?wsdl we will get:

例如您的服务 http://somesite.com/wcf/RealService.svc?wsdl 我们将得到:

  • IRealService (interface)
  • RealServiceClient (implements IRealService and extends System.ServiceModel.ClientBase)
  • IRealService(接口)
  • RealServiceClient(实现 IRealService 并扩展 System.ServiceModel.ClientBase)

You can create a mock class which implements your service interface (IRealService).

您可以创建一个实现您的服务接口 (IRealService) 的模拟类。

And in your application instead of instanciating the concrete service client (RealServiceClient) when you want to call your service you can use a Factory or an IOC container.

在您的应用程序中,当您想要调用您的服务时,您可以使用工厂或 IOC 容器,而不是实例化具体的服务客户端 (RealServiceClient)。

This way you can decide which instance your application (or modules/components) works with: real service in runtime, mock when testing.

通过这种方式,您可以决定您的应用程序(或模块/组件)使用哪个实例:运行时的真实服务,测试时的模拟。