Search code examples
iosiphoneswiftsiesta-swift

iOS - How to stub siesta calls?


I am trying to use siesta in my current app, for all network calls. As usual, I am writing UT ensuring everything is working as expected. I am currently struggling with UT on a siesta PUT call: I am not able to stub the call as I used to with Alamofire.

Here is the code of siesta call:

func rateSession(sessionId: Int, rating: Int, completionHandler: (Bool -> Void)? = nil) {
  Api.shared.resource("session/" + String(sessionId) + "/rate")
    .withParam("user_id", userId)
    .request(.PUT, json: ["rate": rating])
    .onSuccess { _ in
      print("siesta call ok")
      completionHandler?(true)
    }
    .onFailure { _ in
      print("siesta call failed")
      completionHandler?(false)
    }
}

Here is the code used to stub and test the siesta call (using MockingJay):

// Given
let sessionId = 42
let sessionService = SessionService(userId: "42")
var rateSessionSuccess = false

self.stub(http(.PUT, uri: "http://demo.usievents.com/api/v1/session/42/rate?user_id=42"), builder: http(204))

// When
sessionService.rateSession(sessionId, rating: 1) { success in
  rateSessionSuccess = success
}

// Then
expect(rateSessionSuccess).toEventually(beTrue())

Printing request and stub request does not show any difference. Using alamofire instead of siesta is working great (stub is called and I can modify call response to test all expected use cases).


Solution

  • After discussing the issue with a colleague of mine, it appears that Siesta was not using the default NSURLSession. Thus, I just added the default session when initialising Siesta service and everything works fine.

    super.init(baseURL: environment.apiBaseAddress, networking: NSURLSessionConfiguration.defaultSessionConfiguration())