【问题标题】:Mocha unit tests for Firebase appFirebase 应用的 Mocha 单元测试
【发布时间】:2016-12-28 09:40:42
【问题描述】:

我使用 firebase 3.3.0,我想在我的 mocha 单元测试中使用 signInWithEmailAndPassword 函数,但我收到错误 auth/network-request-failed

Unhandled rejection Error: A network error (such as timeout, interrupted connection or unreachable host) has occurred. 

test.js

const FIREBASE_CONFIG = {
    apiKey: "AIzaSyDdA1POUWy9eid1AdBYuMdxch_k8ob7Qrg",
    authDomain: "my-app.firebaseapp.com",
    databaseURL: "https://my-app.firebaseio.com",
    storageBucket: "my-app.appspot.com",
};

const FIREBASE_API_REF = firebase.initializeApp(FIREBASE_CONFIG);

before(function (done) {

        promise = new Promise(function (resolve, reject) {
            return FIREBASE_API_REF.auth().signInWithEmailAndPassword(firstUserEmail, firstUserPassword)
            .then(function (userData) {
                firstUserId = userData.uid;
                resolve(userData);
                done();
            }, function (error) {
                return reject(error);
            })
        });

    });

package.json

"scripts": {
    "test": "mocha --grep ./e2e.js --invert --compilers js:babel-register -R spec --ui bdd --timeout 7000"
  }

【问题讨论】:

  • 您需要显示FIREBASE_API_REF 所指的内容。此外,代码还有其他问题:done 从未被调用; resolve 永远不会被调用;而且您不需要创建自己的承诺 - 那是 antipattern
  • @cartant 感谢您的回复,这不是before 功能的完整部分。我只是想展示一段无法调用signInWithEmailAndPassword 函数的代码。请查看我编辑的问题。
  • 我认为问题的部分问题在于不清楚您在问什么,并且您已经剪掉了部分测试代码 - describeit 调用在哪里?此外,您还没有指定如何运行测试:在使用 Karma 的浏览器中;还是在 Node 中使用 mocha 命令?
  • @cartant 我使用 mocha 命令运行测试(我将此脚本添加到我的原始帖子中)。如果我收到错误 before 函数,describeit 并不重要。我不明白我的问题有什么不清楚的地方。我展示了一段代码,其中我在 firebase 函数中出现错误,我不知道为什么。
  • This Gist 为我工作,可能对您比较有帮助

标签: javascript firebase mocha.js firebase-authentication


【解决方案1】:

当你说,“我想在我的 mocha 单元测试中使用 signInWithEmailAndPassword 函数”那么我会告诉你,“为什么”?

您是否想通过测试他们的身份验证服务是否有效来帮助 Firebase 团队?你真好,但如果你想测试你的应用程序,那么你应该在单元测试中调用 firebase。您真正要检查的是,您的应用程序在响应返回后运行的代码中是否正确处理了类似于 Firebase 响应的响应。

如果我的任务是为此编写测试,我会使用带有 mocha 的 sinon 库并创建一个调用不同函数的存根,该函数返回一些数据而不是实际调用 Firebase:

这说明了 sinon 存根的语法:

var stub = sinon.stub(object, "method", func);

这就是我在你的例子中要做的:

var stub = sinon.stub(FIREBASE_API_REF.auth(), "signInWithEmailAndPassword"
,  () => { 

  // Simply return a JSON object that is similar to the normal response from Firebase
  return {
    name: "Jim",
    data: {
      something: "some stuff"
    }
});

【讨论】:

  • 嘿吉姆 - 我正在考虑如何使用 mocha 测试和数据库来测试 firebase 安全规则。访问登录用户会非常有用。
  • 嗯,您可以执行集成测试之类的操作来验证独立于前端用户界面的安全规则。在您的“快乐案例”测试中,使用虚拟用户帐户登录,尝试访问数据库对象,并断言您可以获得该对象。然后,您可以进行另一个测试,以没有访问权限的虚拟用户身份登录。对他来说,您可以断言对 DB 对象的请求被拒绝(它返回 null 或请求失败等)。祝你好运! :)
【解决方案2】:

可能你不再需要它了,但我没有创建存根,而是使用了 spyOn,它就像一个魅力。

【讨论】:

    【解决方案3】:

    对于其他面临此问题的人,我能够确定问题来自 jsdom 的全局 XMLHttpRequest 对象。通过使用此代码设置我的全局变量,我能够摆脱错误:

    var doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
    global.window = doc.defaultView;
    
    for (var key in window) {
      if (!window.hasOwnProperty(key)) continue;
      if (key in global) continue;
      if (key == 'XMLHttpRequest') continue;
    
      global[key] = window[key];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 2023-04-11
      • 2016-11-19
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多