swiftunit-testingswifty-jsonundefined-symbolxcode14

Undefined Symbol: SwiftyJSON - Unit Testing


I've been looking at how to fix this bug extensively without success. Currently, I'm using Xcode 14. I don't know if this has something to do with these errors.

Basically, they appear when I try to execute the test so doesn't compile the project.

These are the errors

Undefined symbol: :$s10SwiftyJSON0B0V17dictionaryLiteralACSS_yptd_tcfC

Undefined symbol: _$s10SwiftyJSON0B0VMn

My code

Mock (just using the createReport function):
import Foundation
@testable import PurchHist
import SwiftyJSON

struct JsonObject: Codable {
    let subject: String
    let detail: String
}

class MockErrorReport: ReportIssuesServiceProtocol {
    
    var error: ErrorKind?
    let json = JSON()
    
    func createReport(withParams params: JSON, completion: @escaping ReportIssueResponse) {
        completion(error, nil)
    }
    
    func uploadImage(uploadData: UploadData, completion: @escaping ReportIssueImageResponse) {
    }
}
Unit Testing:
 func testSendReport() {
    let mock = MockErrorReport()
    viewModel = .init(reportService: mock)
    
    var capturedResult: Any?
    var capturedError: Error?

    let promise = expectation(description: "Completion handler has been invoked")
    viewModel.reportService.createReport(withParams: mock.json, completion: { error, errors in
        if error == nil && errors == nil {
            capturedResult = true
        } else {
            capturedError = error
        }
        promise.fulfill()
    })
    waitForExpectations(timeout: 5)

    XCTAssertEqual(capturedResult as! Bool, true)
    XCTAssertNil(capturedError)
}

Solution

  • I had the same problem, the problem is the swiftyJSON library, you should have the library installed in the unit test target in your podfileenter image description here.