mardi 3 mai 2016

How to replace a Swift class property for testing

I've got a Swift class that I'm unit testing. The main purpose of the class is to make HTTP calls. I just finished mocking all networking requests using Mockingjay, but I want to make sure that going forward I don't forget to mock future requests. While I was doing the initial mocking I replaced the base URL I'm using with one that doesn't work, but I'd like to keep that in place only for my tests. The class under test looks like this:

public class MyWebServiceWrapper {
    ...

    public class var baseURL: String {
        return "https://api.thesite.com"
    }

    ...
}

I've tried to use OCMock from an Objective-C class to replace baseURL's implementation, and I've tried using method swizzling as described on NSHipster as well (I made the class derive from NSObject and replaced baseURL with a method instead of the property shown above - I'd rather not swizzle, especially since I otherwise don't need the class to be an NSObject subclass).

What's the correct way to achieve my goal of returning an invalid value from baseURL?

Aucun commentaire:

Enregistrer un commentaire