使用Firebase(快速)进行单元测试

时间:2019-09-07 20:13:50

标签: ios swift xcode firebase cocoapods

尝试使用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()
}

Podfile:

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。在网上找不到任何有关单元测试所需的特殊配置的信息。

1 个答案:

答案 0 :(得分:0)

Welp,不能说具体的配置问题是什么,但是最终成为那些“尝试关闭并重新打开”解决方案之一。

pod deintegrate
pod cache clean --all
pod install

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