简体   繁体   中英

How to unit test TwilioChatClient on iOS with Swift and CocoaPods

Since Twilio Programmable Chat SDK is quite complex I sometimes run into scenarios hard to replicate in the app, so I decided to do some unit tests. The problem is, I am unable to initialize the TwilioChatClient . Do you have an idea what am I doing wrong?

Xcode debug console

Test Case '-[ONNTests.ONNTests testTwoClientCreation]' started.
    0x7000065c6000 | 12/18/13:26:18.979 | FATAL    | Chat-iOS | Error instantiating client framework path.
    0x7000065c6000 | 12/18/13:26:18.980 | DEBUG    | Chat-iOS | releasing chat client instance: <TwilioChatClient: 0x7fcf01552c20>

Podfile

target 'MyTests' do
  pod 'TwilioChatClient', '~> 2.1.0'
end

Code

import XCTest
import TwilioChatClient

class MyTests: XCTestCase {
    var client1: TwilioChatClient?

    let e1 = XCTestExpectation(description: "Download Twilio token 1")
    let e2 = XCTestExpectation(description: "Create client 1 successfully")

    func testTwilioClientCreation() {
        TwilioChatClient.setLogLevel(.debug)

        getToken { [unowned self] token in
            self.e1.fulfill()
            TwilioChatClient.chatClient(withToken: token, properties: nil, delegate: self) { [unowned self] (result, client) in
                if result.isSuccessful() {
                    self.client1 = client
                    self.e11.fulfill()
                }
            }
        }

        wait(for: [e1, e2], timeout: 20.0)
    }

    func getToken(completion: @escaping (_ token: String) -> Void) {
        // ...
    }
}

It looks like the TwilioChatClient may be being brought in via the target under test, not the test directly. This can cause an issue in our ability to find resources contained in the framework which is message you are seeing.

To work around this, please ensure the TwilioChatClient framework is declared as a dependency to your test target directly, for example:

target 'TestingSample' do
  pod 'TwilioChatClient', '~> 2.1.0'

  target 'TestingSampleTests' do

  end
end

Please also note it is important to shutdown the client when you are done with it, either at the end of your test method or in a shared tear down method.

wait(for: [e1, e2], timeout: 20.0)

self.client1?.shutdown()
self.client1 = nil

A working sample project can be found here .

We'll work on making this smoother in a future release, please let us know if the above resolves the unit testing issue you're seeing or if we can help further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM