venmo / DVR

Network testing for Swift

Home Page:http://blog.soff.es/network-testing-in-swift-with-dvr

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Testing api failures

OliverDimitrov opened this issue · comments

Hi there,
I'm using DVR, and so far I've managed successfully to create tests when api requests return success. But now I want to test the failure of the same requests. So I'm facing a problem when using DVR to test failures (401, 404 etc)..

When run for 2nd time (first time is to record the response), instead of failure callback, success callback is called with the error response inside of it. Any idea why?

Using Alamofire.
Example code (creating and executing any request):

request.validate().responseJSON(completionHandler: {
    response in
    switch response.result {

         case .success(let JSON):
// This is what gets called instead of failure. Response code is 401 and the JSON contains error message.

              callback(true, JSON, nil)

         case .failure(let error):
              if let data = response.data, data.count > 0 {
                  let JSON = try! JSONSerialization.jsonObject(with: data, options: .allowFragments)
                  callback(false, JSON, error as NSError?)
              } else {
                  callback(false, nil, error as NSError?)
              }
     }
}).resume()

#74 Related?

commented

I think #74 is a duplicate issue and #80 should fix this.

This should be resolved by #75 and #80