菜鸟教程小白 发表于 2022-12-11 18:19:02

ios - Firebase 和 Swift 3 异步单元测试


                                            <p><p>所以我在网上做了一些研究,发现的最佳答案要么已过时,要么适用于 Android。任何帮助表示赞赏! </p>

<p>对于我上周完成的另一个项目,我必须为使用 Swift 2.3 的自定义 Heroku/PostGreSQL 后端编写大约 2 打测试用例。我所做的只是在测试开始时创建一个 asyncExpectation 变量,在完成处理程序执行后实现期望,然后在测试的底部等待它实现。 </p>

<p>现在我将 Firebase 和 Swift 3 用于一个新项目。出于某种原因,当我取消注释下面的 <code>asyncExpectation.fulfill()</code> 时,没有任何内容添加到数据库中。将其注释掉后,一切正常。我应该如何使用 Firebase 和 Swift 3 测试数据存储/检索?</p>

<p>编辑:我应该在我的在线研究中包含这一点,我发现我可能需要使用调度,但这似乎是嵌套完成处理程序的解决方案,而我的数据库 <code>append</code> 方法并不完全一个completionHandler,所以我不知道如何继续。</p>

<pre><code>class RooMate_v2Tests: XCTestCase {
    func testCreateUser() {

      let asyncExpectation = expectation(description: &#34;createUserTestOne&#34;)
      var testSuccess = false

      let testUser = (email: &#34;TESTUSER|&#34; + String().randomString(length: 6) + &#34;@test.com&#34;, password: String().randomString(length: 6), firstName: &#34;jimmy&#34;, lastName: &#34;jenkins&#34;)

      FIRAuth.auth()?.createUser(withEmail: testUser.email, password: testUser.password) { (user, error) in
            if error != nil {
            print(&#34;Error: \(error.debugDescription)&#34;)
            } else {
            let userProfileInfo = [&#34;firstName&#34; : testUser.firstName,
                                 &#34;lastName&#34; : testUser.lastName,
                                 &#34;email&#34; : testUser.email,
                                 &#34;profilePictureURL&#34; : &#34;N/A&#34;]

                let backendRef = FIRDatabase.database().reference()               
                backendRef.child(&#34;users&#34;).child(&#34;TESTUSER|&#34; + user!.uid).setValue(userProfileInfo)
                // testSuccess = true
            }
            // asyncExpectation.fulfill()
      }

      waitForExpectations(timeout: 10) { (error) in
            XCTAssertTrue(testSuccess)
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在我看来,您的问题是您正在等待处理程序中检查 setValue 是否成功。如文档中所述,<strong>只有在超时的情况下才会调用处理程序</strong>:</p>

<blockquote>
<p>A block to be invoked when a call to
-waitForExpectationsWithTimeout:handler: times out or has had all associated expectations fulfilled.</p>
</blockquote>

<p>我建议你检索刚刚设置的值并检查它,如下所示:</p>

<pre><code>let retrievedUserProfile = backendRef.child(&#34;users&#34;).child(&#34;TESTUSER|&#34; + user!.uid)
XCTAssertTrue(retrievedUserProfile == userProfileInfo)
</code></pre>

<p>我不知道这段代码是否正确,只是为了说明我的建议。<br/>
希望对你有帮助!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Firebase 和 Swift 3 异步单元测试,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41091391/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41091391/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Firebase 和 Swift 3 异步单元测试