You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用RxTest测试时遇x86_64/arm64架构未定义符号错误求助

Fixing "Undefined symbols for architecture x86_64/arm64" in RxSwift Tests with RxCocoa

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 TestingTests target
  • Navigate to Build Phases → Link Binary With Libraries
  • Click the + button, search for RxCocoa, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:25:12