MockServer 0.1.0

MockServer 0.1.0

测试已测试
语种语言 Obj-CObjective C
许可协议 MIT
发布最后发布2016年5月

Matthew Patey 维护。



  • Char Software, Inc. d/b/a Localytics

MockServer 是一个捕捉所有接收到的 HTTP 请求的服务器。其主要用途是在测试应用中进行内嵌,以捕获应用程序的网络流量进行验证。尤其是如果您认为产品的一个主要输出是它的网络请求,MockServer 可以用来对产品进行黑盒测试。这在单元测试环境中也很有用,如果您想在仍然使用真实的 HTTP 堆栈的同时测试网络代码片段。作为一个真实的 HTTP 服务器,此工具在向正在测试的产品返回占位数据方面也具有很大的潜在价值。

用法

要运行示例项目,克隆仓库,然后首先从 Example 目录运行 pod install。打开 MockServer.xcworkspace。选择 MockServer-Example 目标并运行单元测试。以下展示了 mock 服务器的用法。有关更详细的示例,请参阅 MockServerTests 类中的测试。注意,MockServer 只支持 http 请求,所以 iOS 9 及以上版本上需要启用 ATS 异常。

@import MockServer; // if using frameworks
#import <MockServer/MockServer.h> // if not using frameworks

// ...

// create and start the server
LLMockServer *server = [[LLMockServer alloc] init];
XCTAssert([server startServer]);

// pass the host and port the server is listening on to code being tested
[uut setAuthority:server.authority];

// do something that cause the code being tested to make a network request
[uut doSomethingOnTheNetwork];

// obtain the request from the server. If the payload is not yet available it will wait for a timeout before returning `nil`
HTTPMessage *message = [server getNextRequest];
XCTAssertNotNil(message);
XCTAssertEqualObjects([message ll_bodyString], expectedPayload);

除了 HTTPMessage+Parsing.h 中的方法,您还可以利用 CocoaHTTPServer 项目HTTPMessage 类的所有功能。

安装

MockServer 通过 CocoaPods 可用。要安装它,只需将以下行添加到您的 Podfile 中:

pod "MockServer"

...

...

...

...

...

...