【问题标题】:Unit Testing with Firebase (Swift)使用 Firebase (Swift) 进行单元测试
【发布时间】:2019-09-07 20:13:50
【问题描述】:

尝试使用 Firebase 进行一些简单的单元测试,但由于某种原因 Firestore 无法初始化:

ModelTests.swift:

func testFirebase() throws
{
    // THIS IS ALWAYS "UNINITIALIZED"
    let db = Firestore.firestore()

    // Because db isn't initialized correctly, this ref will always be nil.
    let ref: DocumentReference? = db.document("unit_testing/xxx")
    ref?.setData([
        "id" : "someID",
        "name" : "goldenjoe",
        "another" : "bmus",
        "testDate" : Date()
        ],
                 merge: true,
                 completion: { err in
                    if let err = err {
                        print("Error adding document: \(err)")
                    } else {
                        print("Document added with ID: \(ref!.documentID)")
                    }
                }
        )
}

AppDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 
{
    // Set up Firestore
    FirebaseApp.configure()
}

Pod 文件:

platform :ios, '11.0'

target 'MyWork' do
use_frameworks!

# ignore all warnings from all pods
inhibit_all_warnings!

# Pods for MyWork
pod 'CoreXLSX'
pod 'Eureka'
pod 'Firebase/Firestore'
pod 'Firebase/Auth'
pod 'ProgressHUD'
#pod 'GoogleSignIn'
pod 'SteviaLayout'

target 'MyWorkTests' do
  inherit! :complete
  end

end

但是,当我正常运行应用程序时,我可以在应用程序的其他部分正常使用 Firebase。在网上找不到任何有关单元测试所需的特殊配置的信息。

【问题讨论】:

    标签: ios swift xcode firebase cocoapods


    【解决方案1】:

    Welp,不能说具体的配置问题是什么,但这最终成为“尝试将其关闭再打开”的解决方案之一。

    pod deintegrate
    pod cache clean --all
    pod install
    

    另外,与文档相反,您需要在 podfile 中包含 Firebase/Core,否则您将无法初始化 Firestore。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-27
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      相关资源
      最近更新 更多