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

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

问题描述

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



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


  1. 创建一个网络服务器向客户端提供静态结果(例如这里)。

  2. 实施不同的网络服务通讯代码,根据编译时间标志调用webservices或代码,从而加载来自本地文件的响应(示例另一个)。



我很好奇社群对每种方法的看法,



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


  1. wronguser@blahblah.com
  2. rightuser@blahblah.com 成功登录。

代码检查用户名参数,并给我一个适当的响应。希望这是我需要在假网络服务的所有逻辑。

解决方案

至于选项1,我在过去使用CocoaHTTPServer和嵌入服务器直接在OCUnit测试中:



https:// github .com / robbiehanson / CocoaHTTPServer



我在单元测试中使用这个代码:
https://github.com/quellish/UnitTestHTTPServer



毕竟,HTTP是通过设计只是请求/响应。



通过创建模拟HTTP服务器或在代码中创建模拟Web服务来模拟Web服务,其工作量大概相同。如果你有X代码路径来测试,你至少有X代码路径来处理你的模拟。



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



[MyMockWebService performLogin:username withPassword:password]
关键点是MyCoolWebService和MyMockWebService实现相同的合同(在目标c中,这将是一个议定书)。 OCMock有大量的文档,让你开始。



对于集成测试,您应该针对真实的Web服务进行测试,例如QA /暂存环境。你实际上描述的声音更像功能测试,而不是集成测试。


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. 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 getting login denied and
  2. rightuser@blahblah.com logging in successfully.

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?

解决方案

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

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

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

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.

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]

would become, in your test

[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.

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.

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

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