XCTestCase - Undefined symbol

Hello, I began writing tests for a SwiftUI project I've been working on, but was getting some "Undefined symbol" error when attempting to run the tests. I thought maybe it was something with the project/target configuration, so I created a brand new SwiftUI sample app, added in the most basic ViewModel, and was able to reproduce the error. You can find the relevant code snippets below. Any idea how to solve this?

ContentViewModel:
Code Block swift
import Foundation
class ContentViewModel: ObservableObject {
    @Published var description: String
    init(description: String) {
        self.description = description
    }
}


ContentView:
Code Block swift
import SwiftUI
struct ContentView: View {
    @StateObject var viewModel: ContentViewModel
    
    var body: some View {
        Text(viewModel.description)
            .padding()
    }
}


ContentViewModelTests:
Code Block swift
import XCTest
@testable import Sample
class ContentViewModelTests: XCTestCase {
    func testDescription() throws {
        let viewModel = ContentViewModel(string: "Hello, world!")
        XCTAssertEqual(viewModel.string, "Hello, world!")
    }
}


Error:
Code Block
Undefined symbols for architecture arm64:
  "type metadata accessor for Sample.ContentViewModel", referenced from:
      Tests_iOS.ContentViewModelTests.testDescription() throws -> () in ContentViewModelTests.o
  "Sample.ContentViewModel.__allocating_init(string: Swift.String) -> Sample.ContentViewModel", referenced from:
      Tests_iOS.ContentViewModelTests.testDescription() throws -> () in ContentViewModelTests.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


Accepted Reply

Turns out the default tests created by Xcode are actually UITests. Creating a brand new test target fixed this.

Replies

Turns out the default tests created by Xcode are actually UITests. Creating a brand new test target fixed this.