在Alamofire应用中对HTTP流量进行单元测试 [英] Unit Testing HTTP traffic in Alamofire app

查看:98
本文介绍了在Alamofire应用中对HTTP流量进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找如何最好地测试使用Alamofire来帮助与服务器数据同步的应用程序。

I'm struggling a bit to figure out how to best test an app that uses Alamofire to help sync with server data.

我希望能够进行测试我的代码使用Alamofire并处理来自服务器的JSON响应。
我想模拟那些测试,以便我可以将期望的响应数据提供给这些测试,而不会产生实际的网络流量。

I want to be able to test my code that uses Alamofire and processes JSON responses from a server. I'd like to mock those tests so that I can feed the expected response data to those tests without incurring real network traffic.

此博客文章(< a href = http://nshipster.com/xctestcase/ rel = noreferrer> http://nshipster.com/xctestcase/ )描述了在Swift中模拟对象是多么容易-但我我不确定如何使用Alamofire及其链接的响应来执行此操作。

This blog post (http://nshipster.com/xctestcase/) describes how easy it is to Mock an object in Swift - but I'm not sure how to do that with Alamofire and its chained responses.

我会嘲笑Manager吗?请求?响应?

Would I mock the Manager? the Request? Response? Any help would be appreciated!

推荐答案

我添加了另一个答案,因为我发现这种方法在我看来是更容易阅读和使用。

I'm adding another answer since I've just found this approach that in my opinion is easier and really simple to read and use.

我创建了一个虚拟的Alamofire类,该类仅包含测试所需的功能和类型。
现在,我将此文件包括在测试目标中,而不是真正的Alamofire。

I've created a dummy Alamofire class that contains only the functions and the types necessary for tests. Now I include this file in the test target instead of the real Alamofire.

例如,我创建了 Request 类的版本,在其中定义了几个静态变量,视测试而定,对于该类,我仅实现了 init responseJSON 函数。

For example I've created my version of the Request class where I define a couple of static variables that I valorise depending on the test, and for this class I've implemented only the init and the responseJSON function.

public class Request {

    var request:String?
    struct response{
        static var data:NSHTTPURLResponse?
        static var json:AnyObject?
        static var error:NSError?
    }

    init (request:String){
        self.request = request
    }

    public func responseJSON(options: NSJSONReadingOptions = .AllowFragments, completionHandler: (NSURLRequest, NSHTTPURLResponse?, AnyObject?, NSError?) -> Void) -> Self {

        completionHandler(NSURLRequest(URL: NSURL(string:self.request!)!), Request.response.data, Request.response.json, Request.response.error)
        return self
    }
}

现在我可以在测试中模拟响应:

Now I can mock a response in a test:

func testMytestFunction(){
    var HTMLResponse = NSHTTPURLResponse(URL: NSURL(string: "myurl")!, statusCode: 200, HTTPVersion: "HTTP/1.1", headerFields: nil)

    Request.response.data = HTMLResponse
    Request.response.json = LoadDataFromJSONFile("MyJsonFile")

    request(.POST, "myurl", parameters: nil, encoding: ParameterEncoding.JSON).responseJSON {
        (request, response, JSON, error) -> Void in
        // the JSON and response variable now contains exactly the data that you have passed to Request.response.data and Request.response.json
    }
}

请求功能在此处定义:

public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request {

    return Request(request: URLString.URLString)
}

public func request(URLRequest: URLRequestConvertible) -> Request {

    return Request(request: "fakecall")
}

这篇关于在Alamofire应用中对HTTP流量进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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