11import Foundation
22import XCTest
3+ import OHHTTPStubs
34
45@testable import WordPressKit
56
@@ -67,8 +68,15 @@ final class CommentServiceRemoteREST_APIv2Tests: RemoteTestCase, RESTTestable {
6768 wait ( for: [ expect] , timeout: timeout)
6869 }
6970
70- func test_getCommentsV2_correctlyPassesCustomParameters( ) {
71- let mockApi = MockWordPressComRestApi ( )
71+ func test_getCommentsV2_correctlyPassesCustomParameters( ) throws {
72+ let requestReceived = expectation ( description: " HTTP request is received " )
73+ var request : URLRequest ?
74+ stub ( condition: isHost ( " public-api.wordpress.com " ) ) {
75+ request = $0
76+ requestReceived. fulfill ( )
77+ return HTTPStubsResponse ( error: URLError ( . networkConnectionLost) )
78+ }
79+
7280 let expectedParentId = 4
7381 let expectedAuthorId = 5
7482 let expectedContext = " edit "
@@ -77,20 +85,22 @@ final class CommentServiceRemoteREST_APIv2Tests: RemoteTestCase, RESTTestable {
7785 . author: expectedAuthorId,
7886 . context: expectedContext
7987 ]
80- remote = CommentServiceRemoteREST ( wordPressComRestApi: mockApi, siteID: NSNumber ( value: siteId) )
81-
88+ remote = CommentServiceRemoteREST ( wordPressComRestApi: WordPressComRestApi ( ) , siteID: NSNumber ( value: siteId) )
8289 remote. getCommentsV2 ( for: siteId, parameters: parameters, success: { _ in } , failure: { _ in } )
90+ wait ( for: [ requestReceived] , timeout: 0.3 )
8391
84- XCTAssertNotNil ( mockApi. parametersPassedIn)
85- XCTAssertTrue ( ( mockApi. parametersPassedIn! as? [ String : AnyObject ] ) != nil )
92+ let url = try XCTUnwrap ( request? . url)
93+ let queryItems = try XCTUnwrap ( URLComponents ( url: url, resolvingAgainstBaseURL: true ) ? . queryItems)
94+ let params = queryItems. reduce ( into: [ String: String] ( ) ) { result, query in
95+ result [ query. name] = query. value
96+ }
8697
87- let params = mockApi. parametersPassedIn! as! [ String : AnyObject ]
8898 XCTAssertNotNil ( params [ CommentServiceRemoteREST . RequestKeys. parent. rawValue] )
89- XCTAssertEqual ( params [ CommentServiceRemoteREST . RequestKeys. parent. rawValue] as! Int , expectedParentId)
99+ XCTAssertEqual ( params [ CommentServiceRemoteREST . RequestKeys. parent. rawValue] , expectedParentId. description )
90100 XCTAssertNotNil ( params [ CommentServiceRemoteREST . RequestKeys. author. rawValue] )
91- XCTAssertEqual ( params [ CommentServiceRemoteREST . RequestKeys. author. rawValue] as! Int , expectedAuthorId)
101+ XCTAssertEqual ( params [ CommentServiceRemoteREST . RequestKeys. author. rawValue] , expectedAuthorId. description )
92102 XCTAssertNotNil ( params [ CommentServiceRemoteREST . RequestKeys. context. rawValue] )
93- XCTAssertEqual ( params [ CommentServiceRemoteREST . RequestKeys. context. rawValue] as! String , expectedContext)
103+ XCTAssertEqual ( params [ CommentServiceRemoteREST . RequestKeys. context. rawValue] , expectedContext)
94104 }
95105
96106 func test_getCommentsV2_givenEditContext_parsesAdditionalFields( ) {
0 commit comments