Unit test Swift with third party library

771 views Asked by At

I am using Localytics in my app which is basically just an API that can be used to send analytics data to the server.
https://docs.localytics.com/dev/ios.html#install-sdk-ios

I work in a project where we have to unit test every single file to make sure that there is atleast 90% code coverage.
Since localytics is an external library, I have a wrapper around it to use the API.

Consider the simple localytics method :

func tagScreen(screenName: String) {
    Localytics.tagScreen(screenName) 
}

How can I unit test this wrapper?
Is there any suggestions?
How can we write Mocks for the method above?

1

There are 1 answers

0
Viktoras Laukevičius On

If you want to test the wrapper: as Localytics has static functions to interact with I would go with something like:

class LocalyticsWrapper {
  private let tagFunc: (String) -> Void
  init(tagFunc: @escaping (String) -> Void = Localytics.tagScreen) {
    self.tagFunc = tagFunc
  }
  func tagScreen(screenName: String) {
    // maybe some pre-formatting done here or something
    tagFunc(screenName) 
  }
}

Now you can inject mock function and test the wrapper