让 Alamofire 和 OHHTTPStubs 一起工作
Posted
技术标签:
【中文标题】让 Alamofire 和 OHHTTPStubs 一起工作【英文标题】:Getting Alamofire and OHHTTPStubsWorking together 【发布时间】:2016-04-01 16:55:29 【问题描述】:我正在尝试设置 OHHTTPStubs 以与 Alamofire 一起进行单元测试,但在使用 Alamofire 时它们似乎总是从实时网络加载。我已关闭在测试目标中使用主机应用程序,并确保首先使用 OHHTTPStubs。
这是一个示例测试,其中从 Alamofire 加载的结果来自实时网络:
import XCTest
import OHHTTPStubs
import Alamofire
@testable import TestAlamoFireStubs
class TestAlamoFireStubsTests: XCTestCase
let responseText = "'data':'val'"
override func setUp()
super.setUp()
stub(isHost("httpbin.org")) request -> OHHTTPStubsResponse in
let stubData = self.responseText.dataUsingEncoding(NSUTF8StringEncoding)
return OHHTTPStubsResponse(data:stubData!, statusCode:200, headers:nil)
func testNSURLSession()
let expectation = expectationWithDescription("Check NSURLSession")
let url = NSURL(string:"https://httpbin.org/get")
let dataTask = NSURLSession.sharedSession().dataTaskWithURL(url!) data, response, error in
let responseString = NSString(data:data!, encoding:NSUTF8StringEncoding) as? String
XCTAssertEqual(responseString, self.responseText) // succeeds
expectation.fulfill()
dataTask.resume()
waitForExpectationsWithTimeout(10, handler:nil)
func testAlamofire()
let expectation = expectationWithDescription("Check Alamofire")
Alamofire.request(.GET, "https://httpbin.org/get").response request, response, data, error in
let responseString = NSString(data:data!, encoding:NSUTF8StringEncoding) as? String
XCTAssertEqual(responseString, self.responseText) // fails
expectation.fulfill()
waitForExpectationsWithTimeout(10, handler:nil)
还有一个示例项目的链接: https://www.dropbox.com/s/b0qdvjpk8t6r525/TestAlamoFireStubs.zip
【问题讨论】:
【参考方案1】:至少在我的特殊情况下,我能够通过使用自定义 NSURLSession 和配置创建自定义管理器来将 Alamofire 绑定到 OHHTTPStubs
import XCTest
import Alamofire
import OHHTTPStubs
@testable import AlamofireOHHTTPStubs
class AlamofireOHHTTPStubsTests: XCTestCase
let manager: Manager =
let configuration = NSURLSession.sharedSession().configuration
//The most important string!
OHHTTPStubs.setEnabled(true, forSessionConfiguration: configuration)
configuration.URLCache = nil
let delegate = Manager.SessionDelegate()
let session = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
return Manager(session: session, delegate: delegate)!
()
let responseText = "\"data\":\"val\""
func testAlamofireOHHTTPStubs()
stub(isHost("httpbin.org")) request -> OHHTTPStubsResponse in
let stubData = self.responseText.dataUsingEncoding(NSUTF8StringEncoding)
return OHHTTPStubsResponse(data:stubData!, statusCode:200, headers:nil)
let expectation = expectationWithDescription("alamofire request expectation")
manager.request(.GET, "https://httpbin.org/get").responseJSON (response) in
switch response.result
case .Success(let json as NSDictionary):
XCTAssertEqual(json["data"] as? String, "val")
default:
XCTFail()
expectation.fulfill()
waitForExpectationsWithTimeout(1.0, handler: nil)
【讨论】:
我为这个问题苦苦挣扎了好几个小时。只有创建具有自定义会话和配置的自定义管理器才有技巧。谢谢@Fyodor Volchyok【参考方案2】:对我来说,问题是我的 Podfile 丢失了 pod 'OHHTTPStubs'
,而我那里只有 pod 'OHHTTPStubs/Swift'
。正确的 Podfile 应该是这样的:
pod 'OHHTTPStubs' # This one required!!
pod 'OHHTTPStubs/Swift' # This one optional
这一切都记录在OHTTPStubs README中。
【讨论】:
【参考方案3】:如果有人想知道如何在 Alamofire 4 和 Swift 3 中设置自定义管理器,这里是解决方案:
let sessionManager: SessionManager
init()
let configuration = URLSessionConfiguration.default
OHHTTPStubs.setEnabled(true, for: configuration)
configuration.urlCache = nil
sessionManager = SessionManager(configuration: configuration)
【讨论】:
以上是关于让 Alamofire 和 OHHTTPStubs 一起工作的主要内容,如果未能解决你的问题,请参考以下文章