如何使用 Visual Studio 2008 对 C# Web 服务进行单元测试 [英] How to unit test C# Web Service with Visual Studio 2008

查看:25
本文介绍了如何使用 Visual Studio 2008 对 C# Web 服务进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您应该如何使用 Visual Studio 2008 在 C# 中对 Web 服务进行单元测试?当我生成单元测试时,它会添加对 Web 服务类的实际引用,而不是 Web 引用.它设置指定的属性:

How are you supposed to unit test a web service in C# with Visual Studio 2008? When I generate a unit test it adds an actual reference to the web service class instead of a web reference. It sets the attributes specified in:

http://msdn.microsoft.com/en-us/library/ms243399(VS.80).aspx#TestingWebServiceLocally

然而,它会在不执行测试的情况下完成.我试图将调用添加到 WebServiceHelper.TryUrlRedirection(...) 但调用不喜欢目标,因为它继承自 WebService,而不是 WebClientProtocol.

Yet, it will complete without executing the test. I attempted to add the call to WebServiceHelper.TryUrlRedirection(...) but the call does not like the target since it inherits from WebService, not WebClientProtocol.

推荐答案

我通常做的不是直接针对 web 服务进行测试,而是尝试在服务中放置尽可能少的代码,并调用不同的类它完成了所有真正的工作.然后我为另一个类编写单元测试.事实证明,类有时在 Web 服务上下文之外也很有用,所以通过这种方式 - 您获得了两倍.

What I usually do is not test directly against the web-service, but to try and put as little code as possible in the service, and call a different class which does all the real work. Then I write unit tests for that other class. It turns out that class can sometimes be useful outside of the web-service context, so this way - you gain twice.

这篇关于如何使用 Visual Studio 2008 对 C# Web 服务进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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