0

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).

Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48
GUL
  • 207
  • 1
  • 8
  • Siesta just uses `NSURLConnection`, which MockingJay supports, so most likely your stub does not match the call Siesta is making. If you do `stub(everything, failure(NSError()))`, then does that intercept the Siesta call? If you enable full Siesta logging, does the HTTP it’s making exactly match your stub? – Paul Cantrell May 03 '16 at 02:34

1 Answers1

1

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())

GUL
  • 207
  • 1
  • 8