有时会模拟网络服务调用

发布于 2024-08-17 13:54:09 字数 394 浏览 2 评论 0原文

我正在尝试为现有应用程序编写验收测试。

不过,当我调用一个网络服务时,我遇到了一个问题,该服务告诉我们一个人是否在办公室,简而言之,是否在办公室、工作时间以及备份人员是谁。

在大多数测试中,实际调用 Web 服务是可以的...是的,理想情况下不应该,但是为多次调用此服务创建输入和输出是一项艰巨的任务。

我想做的是让模拟生成一个默认结果,无论输入如何,但它需要由基于参数的代码生成,因为调用和结果中存在临时数据。

而且,如果我选择,能够在测试场景的测试中对方法的一些选择输入设置不同的结果。

基本上,默认情况下,人们都在办公室。除非我设置模拟让他们不这样。

我可以用最小起订量做到这一点吗?又如何呢?

我对编写测试和模拟还很陌生,所以如果您需要更多说明,请询问。

I am trying to write acceptance testing for an existing app.

I've run into a problem though when calling a web service that tells us if a person is, in short, in the office or not, what hours, and who the backup is.

In most of the tests, actually calling the web service is fine... yes, ideally it shouldn't, but creating inputs and outputs for the many many times this service is called is a HUGE task.

What I'd like to do is have the Mock generate a default result regardless of the input, but it will need to be generated by code based on the parameters as there is temporal data in the call and result.

And, if I choose, to be able to setup a different result on a few select inputs to the method on a test by test scenario.

Basically, by default, people are in the office. Unless I setup the mock for them not to be.

Can I do that with Moq? And how?

I'm pretty new to writing tests and mocking so if you require more clarification, please ask.

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

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

发布评论

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

评论(3

挽手叙旧 2024-08-24 13:54:09

您也许可以使用 Moq 或其他动态 Mock 来做到这一点,但这听起来不是一个好主意。

从您描述需求的方式来看,您似乎希望 Web 服务包含大量逻辑 - 也许不像最终生产 Web 服务那样复杂,但至少是一组启发式规则。在我看来,这听起来很像 Fake 而不是 模拟

简而言之,Fake 是依赖项的轻量级实现。

另一方面,Mock 为输入提供或多或少预先固定的静态答案,同时验证输入是否是预期的。这听起来根本不像你所描述的那样。

简而言之,实现您所描述的内容的最佳方法是编写一个按照您所描述的方式运行的 Web 服务的轻量级版本。如果您需要生成一些虚假的测试数据,可以考虑使用AutoFixture

You may be able to do that with Moq or another dynamic Mock, but it doesn't sound like it would be a good idea.

The way you describe your needs it sounds like you want the web service to contain a non-trivial amount of logic - perhaps not as complex logic as the final production web service, but a set of heuristic rules at least. That sounds to me a lot like a Fake instead of a Mock.

In short, a Fake is a lightweight implementation of the dependency.

A Mock, on the other hand, provides more or less pre-canned, static answers to input while verifying whether the input was expected or not. That doesn't sound at all like what you describe.

In short, the best way to achieve what you describe is to write a lightweight version of the web service that act as you describe. If you need to generate some bogus test data, you can consider using AutoFixture.

秋叶绚丽 2024-08-24 13:54:09

模拟和存根之间存在被误解的差异 [Martin Forler:模拟不是存根 (http:// martinfowler.com/articles/mocksArentStubs.html]
基本上,模拟对象充当具有允许您验证在某些测试场景(主要是单元测试)期间测试的方法是否正确使用对象的机制的对象(即验证测试的方法是否正确调用模拟对象的方法)命令,设置参数并访问属性三次...)。模拟的目的是测试单元测试中的方法是否正确使用超出其边界的其他对象。
另一方面,存根正在模拟某些行为(我认为这就是您所寻求的)。

There is a misunderstood difference bethween mocks and stubs [Martin Forler: Mock Aren't Stubs (http://martinfowler.com/articles/mocksArentStubs.html].
Basically the mock objects serves as the objects with the mechanism that allows you to verify that the method tested during some testing scenario (mostly the Unit tests) is using the object correctly (ie. verify that the tested method called the mock object's methods in correct order, set the parameters and accessed the property three times ...). The mock serves for purpose of testing the method within the unit test whether it is using another objects that are beyond it's boundaries correctly.
On the other hand the stubs are simulating some behavior (this is what I think you seek).

半葬歌 2024-08-24 13:54:09

您可以尝试来自 sourceForge 的基于网络的模拟实用程序。该应用程序允许根据特定的输入标签值配置选定的响应。

http://sourceforge.net/projects/easymocker

Web Service Mocker 是一个易于使用、完全基于 Web 的SOAP Web 服务模拟实用程序。该实用程序在 SOA 开发环境中的单元测试、组件集成测试和非功能需求测试期间非常有用。

You could try the web based mocking utility from sourceForge. The application allows to configure selected response based on a particula input tag value.

http://sourceforge.net/projects/easymocker

Web Service Mocker is an easy to use, completely web based SOAP web service mocking utility. This utility is found very useful in a SOA development environment during unit test, component integration test and non-functional requirement testing.

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