AliSoftware / OHHTTPStubs

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
MIT License
5.03k stars 601 forks source link

OHHTTPStubs not working with Siesta #285

Open kamaki94 opened 6 years ago

kamaki94 commented 6 years ago

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

Liquidsoul commented 5 years ago

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 👌

ahbou commented 5 years ago

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"])
                        }