如何使用 Visual Studio 2008 对 C# Web 服务进行单元测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/371961/
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-04 00:55:55  来源:igfitidea点击:

How to unit test C# Web Service with Visual Studio 2008

c#web-servicesunit-testing

提问by Steven Behnke

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:

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

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

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

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.

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

采纳答案by Doron Yaacoby

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.

我通常做的不是直接针对 Web 服务进行测试,而是尝试在服务中放置尽可能少的代码,并调用一个不同的类来完成所有实际工作。然后我为另一个类编写单元测试。事实证明,类有时在 Web 服务上下文之外很有用,所以这样 - 你会获得两倍。

回答by JoshBerke

You can add a service reference to your unit test project or generate your client stub and put the class in your unit test project.

您可以将服务引用添加到您的单元测试项目或生成您的客户端存根并将该类放入您的单元测试项目中。

回答by Chris Brandsma

If you are writing a web service, try to put all logic in another (testable) layer. Each Web method should have a little code as possible. Then you will have little reason to test the web method directly because you can test the underlying layers.

如果您正在编写 Web 服务,请尝试将所有逻辑放在另一个(可测试的)层中。每个 Web 方法都应该有尽可能少的代码。那么你就没有什么理由直接测试 web 方法了,因为你可以测试底层。

[WebMethod]
public void DoSomething()
{ 
   hander.DoSomething();
}

If you are consuming a web method, wrap the generated caller in a class wrapper, and implement an interface for the class wrapper. Then, anytime you need to call the web service, use the interface to call the method. You want to use the interface so as to make the class wrapper swappable during testing (using Rhino Mocks, Moq, or TypeMock).

如果您正在使用 Web 方法,请将生成的调用者包装在类包装器中,并为类包装器实现接口。然后,任何时候需要调用 Web 服务时,都可以使用该接口调用该方法。您希望使用该接口使类包装器在测试期间可交换(使用 Rhino Mocks、Moq 或 TypeMock)。

回答by Sarah Vessels

Above my web method unit tests, I have the following:

在我的 Web 方法单元测试之上,我有以下内容:

// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
// whether you are testing a page, web service, or a WCF service.
[HostType("ASP.NET")]
[UrlToTest("http://localhost/MyWebService")]

In addition to the usual:

除了通常的:

[TestMethod()]
[DeploymentItem("MyWebService.dll")]

This code came about from using the Visual Studio 2008 Unit Test Wizard.

此代码来自使用 Visual Studio 2008 单元测试向导。

回答by Ra.

回答by csharpforevermore

Know that there are two types of Web Service. Those you write yourself and want to test, and those that you consume. For the former, the above rules apply. However, I would say that sometimes I see developers testing against external web services. Logic dictates that a third party service is unreliable and therefore requires much more testing. In object-oriented programming, it is best to understand the separation of concern that Martin Fowler and the others all told us about. This means that we should not test systems external to our own.

知道有两种类型的 Web Service。那些你自己编写并想要测试的,以及那些你消费的。对于前者,上述规则适用。但是,我会说有时我会看到开发人员针对外部 Web 服务进行测试。从逻辑上讲,第三方服务是不可靠的,因此需要进行更多的测试。在面向对象编程中,最好理解 Martin Fowler 和其他人都告诉我们的关注点分离。这意味着我们不应该测试我们自己的外部系统。

However, I like to write wrapper classes to provide useful functionality against the services. For example, Bing Maps has a number of amazingly powerful functions. I write tests against these just to ensure that they give me the expected values. Although not extensive, the point of them is that if the web service dies for any reason (authentication key expires, etc) then I can be informed of it via the Test Server.

但是,我喜欢编写包装类来为服务提供有用的功能。例如,必应地图有许多惊人的强大功能。我针对这些编写测试只是为了确保它们给我预期的值。尽管不是很广泛,但它们的要点是,如果 Web 服务因任何原因(身份验证密钥过期等)而终止,那么我可以通过测试服务器获知它。