AliSoftware / OHHTTPStubs

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OHHTTPStubs not working with Siesta

kamaki94 opened this issue · comments

I have a data manger that gets a json file from the server an I want to test that call using my own json file . I am using Siesta for API and OHHTTPStubs for stubbing , but for some reason, the stubbing doesn't work.Here the step I did to use it :

I added OHHTTPStubs to pod file

pod 'OHHTTPStubs/Swift'

In test method I wrote this:

func testGetData(){
    stub(condition: isPath("https://www.test.com/data/data.json")) { request in
        let stubPath = OHPathForFile("testData.json", type(of: self)) // break point
        return fixture(filePath: stubPath!, headers: ["Content-Type":"application/json"])
    }
    let expectation = self.expectation(description: "calls the callback with a resource object")
    manager.getData(onSuccess: { (data) in
        XCTAssertGreaterThan(data.count, 0, "Failed to get data")
        expectation.fulfill()
    }) { (error) in
        XCTFail()
    }
    self.waitForExpectations(timeout: 0.5, handler: .none)
}

I put a breakpoint inside the closure of the stub but it doesn't enter it. I also tried isHost("test.com") and is isMethodGET() but still nothing :(

PS : the path for the json file is : test.com/data/data.json

Environment

  • OHHTTPStubs 6.1.0
  • Siesta 1.4.0
  • Cocoapods 1.5.0
  • XCode 9.4
  • Swift 4

Hi @kamaki94

Sorry for the late reply but did you try using a closure in condition to see if a request is at least captured by OHHTTPStubs?

I cannot help you more as you do not provide the implementation of your manager.getData call and I did not try OHHTTPStubs with Siesta myself.
If you are still experiencing the issue, it would be great if you could give us a sample project reproducing it 👌

I've hit the same issue even on 8.0 and the solution is to avoid using the helper method isPath.

Instead pass a closure and compare the URLs:

                        let targetURL = self.apiClient.signin.url.absoluteString
                        
                        stub(condition: { (request) -> Bool in
                            if let absoluteURL = request.url?.absoluteString {
                                return absoluteURL == targetURL
                            }
                            return false
                        }) { _ in
                            return OHHTTPStubsResponse(data: stubData , statusCode: 200, headers: ["Content-Type": "application/json"])
                        }