为 iOS 应用程序存根/模拟 Web 服务 [英] Stubbing/mocking up webservices for an iOS app

查看:15
本文介绍了为 iOS 应用程序存根/模拟 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 iOS 应用程序,其主要目的是与一组远程 Web 服务进行通信.对于集成测试,我希望能够针对某种具有可预测结果的虚假 Web 服务运行我的应用程序.

I'm working on an iOS app whose primary purpose is communication with a set of remote webservices. For integration testing, I'd like to be able to run my app against some sort of fake webservices that have a predictable result.

到目前为止,我已经看到了两个建议:

So far I've seen two suggestions:

  1. 创建一个为客户端提供静态结果的网络服务器(例如 这里).
  2. 实现不同的网络服务通信代码,基于编译时间标志将调用网络服务或从本地文件加载响应的代码(示例另一个).
  1. Create a webserver that serves static results to the client (for example here).
  2. Implement different webservice communication code, that based on a compile time flag would call either webservices or code that would load responses from a local file (example and another one).

我很好奇社区对每种方法的看法以及是否有任何工具可以支持这种工作流程.

I'm curious what the community thinks about each of this approaches and whether there are any tools out there to support this workflow.

更新:那么让我提供一个具体的例子.我有一个需要用户名和密码的登录表单.我想检查两个条件:

Update: Let me provide a specific example then. I have a login form that takes a username and password. I would like to check two conditions:

  1. wronguser@blahblah.com 登录被拒绝和
  2. rightuser@blahblah.com 登录成功.
  1. wronguser@blahblah.com getting login denied and
  2. rightuser@blahblah.com logging in successfully.

所以我需要一些代码来检查 username 参数并向我抛出适当的响应.希望这就是我在假网络服务"中需要的所有逻辑.如何干净利落地管理?

So I need some code to check the username parameter and throw an appropriate response at me. Hopefully that's all the logic that I need in the "fake webservice". How do I manage this cleanly?

推荐答案

就选项 1 而言,我过去曾使用 CocoaHTTPServer 完成此操作,并将服务器直接嵌入到 OCUnit 测试中:

As far as option 1, I have done this in the past using CocoaHTTPServer and embedding the server directly in an OCUnit test:

https://github.com/robbiehanson/CocoaHTTPServer

我在这里提供了在单元测试中使用它的代码:https://github.com/quellish/UnitTestHTTPServer

I put up the code for using this in a unit test here: https://github.com/quellish/UnitTestHTTPServer

毕竟,HTTP 在设计上只是请求/响应.

After all, HTTP is by design just request/response.

模拟 Web 服务,无论是通过创建一个模拟 HTTP 服务器还是在代码中创建一个模拟 Web 服务,都将是大致相同的工作量.如果您有 X 代码路径要测试,那么您的模拟中至少有 X 代码路径要处理.

Mocking a web service, wether by creating a mock HTTP server or creating a mock web service in code, is going to be about the same amount of work. If you have X code paths to test, you have at least X code paths to handle in your mock.

对于选项 2,要模拟 Web 服务,您不会与 Web 服务通信,而是使用具有已知响应的模拟对象.[MyCoolWebService performLogin:username withPassword:password]

For option 2, to mock the web service you would not be communicating with the web service, you would be instead be using the mock object which has known responses. [MyCoolWebService performLogin:username withPassword:password]

在你的测试中会变成

[MyMockWebService performLogin:username withPassword:password]关键是 MyCoolWebService 和 MyMockWebService 实现了相同的合约(在 Objective-c 中,这将是一个协议).OCMock 有大量文档可以帮助您入门.

[MyMockWebService performLogin:username withPassword:password] The key point being that MyCoolWebService and MyMockWebService implement the same contract (in objective-c, this would be a Protocol). OCMock has plenty of documentation to get you started.

不过,对于集成测试,您应该针对真实的 Web 服务进行测试,例如 QA/staging 环境.您实际描述的内容听起来更像是功能测试而不是集成测试.

For an integration test though, you should be testing against the real web service, such as a QA/staging environment. What you are actually describing sounds more like functional testing than integration testing.

这篇关于为 iOS 应用程序存根/模拟 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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