使用RxTest测试时遇x86_64/arm64架构未定义符号错误求助
Let's break down what's happening here and walk through how to fix it:
The Root Cause
Your test code is interacting with a Driver type (a RxCocoa-specific wrapper) via asObservable(), but your test target (TestingTests) isn't linked against the RxCocoa framework. The linker errors you're seeing are because it can't find RxCocoa-exclusive symbols like DriverSharingStrategy or SharedSequence.asObservable()—these aren't part of the base RxSwift library.
Step-by-Step Fixes
1. Add RxCocoa to Your Test Target (Manual Setup)
If you're managing dependencies without CocoaPods:
- Open your Xcode project, select the
TestingTeststarget - Navigate to Build Phases → Link Binary With Libraries
- Click the
+button, search forRxCocoa, and add it to the list of linked frameworks.
2. Update Your Podfile (CocoaPods Users)
Your test target is missing the RxCocoa dependency. Update your Podfile to include it alongside RxSwift and RxTest:
target 'Testing' do use_frameworks! pod 'RxSwift' pod 'RxCocoa' end target 'TestingTests' do inherit! :search_paths use_frameworks! pod 'RxSwift' pod 'RxCocoa' # This line was likely missing! pod 'RxTest' end
Run pod install in your terminal to apply the changes, then re-open your Xcode workspace.
3. (Optional) Refactor the Test for RxTest Best Practices
Instead of using toBlocking(), leverage TestScheduler (from RxTest) for more reliable, time-controlled testing—this aligns better with the patterns taught in the Raywenderlich book. Here's an improved version of your test:
import XCTest import RxSwift import RxTest @testable import Testing class TestingViewModelTests : XCTestCase { // Renamed for clarity var viewModel: ViewModel! var disposeBag = DisposeBag() // Clean up subscriptions automatically override func setUp() { super.setUp() viewModel = ViewModel() } func testColorNameIsRayWenderlichGreenWhenHexStringIs006636() { let scheduler = TestScheduler(initialClock: 0) let colorNameObserver = scheduler.createObserver(String.self) // Subscribe to the colorName Driver as an observable viewModel.colorName.asObservable() .subscribe(colorNameObserver) .disposed(by: disposeBag) // Schedule the hexString update at virtual time 100 scheduler.scheduleAt(100) { self.viewModel.hexString.value = "#006636" } // Start the scheduler to run the test sequence scheduler.start() // Verify we get the expected next event XCTAssertEqual(colorNameObserver.events, [ .next(100, "rayWenderlichGreen") ]) } }
Why This Works
By linking RxCocoa to your test target, you give the linker access to all the RxCocoa-specific symbols your test code needs to run. The RxTest scheduler approach also makes your tests more maintainable, as it lets you simulate time-based events precisely.
内容的提问来源于stack exchange,提问作者Hamed Ghadirian

