1

我想使用 Web Component Tester 测试以下聚合物元素(纸元素的许多导入行和 firebase-auth 已删除)。

<dom-module id="my-login">
  <template>
    <firebase-auth id="auth" app-name="myapp" provider="email"></firebase-auth>
    <paper-input id="email" label="Enter Email"></paper-input>
    <paper-input id="password" label="Enter password" type="password"></paper-input>
    <paper-button id="signin" on-tap="_signIn" raised primary>Login</paper-button>
    <paper-button id="signup" on-tap="_register" secondary>Register</paper-button>
  </template>
  <script>
    Polymer({
      is: 'my-login',
      ready: function () {
        this.$.email.value = "xxxxxxxxxxxxxxx";
        this.$.password.value = "zzzzzzzzzzz";
      },
      _signIn: function () {
        const email = this.$.email.value;
        const passw = this.$.password.value;
        const sgn = this.$.auth;
        sgn.signInWithEmailAndPassword(email, passw) // *** ERRROR HERE ***
          .then(response => {
          });
      }
    });
  </script>
 </dom-module>

使用以下测试套件(删除了许多不相关的细节):

<!doctype html>
<html lang="en"> 
<head>
  <script src="../bower_components/webcomponentsjs/webcomponents-lite.js></script>
  <script src="../bower_components/web-component-tester/browser.js"></script>
  <link rel="import" href="../src/my-login.html">
</head>
<body>
  <test-fixture id="login">
    <template>
      <my-login></my-login>
    </template>
  </test-fixture>
  <script>
  suite('LOGIN', function () {
    var el, loginBtn;

    setup(function () {
      el = fixture("login");
      loginBtn = el.$$('#signin');
    });

    test('user login', done => {
      loginBtn.click();
      flush(_ => {
        done();
      });
    });
  });
  </script>
</body>
</html>

但测试失败并出现以下错误:

Error: Cannot read property 'signInWithEmailAndPassword' of undefined
HTMLElement.signInWithEmailAndPassword at /bower_components/polymerfire/firebase-auth.html:211
HTMLElement._signIn at /src/my-login.html:20

我注意到错误说

Cannot read property signInWithEmailAndPassword of undefined

代替

Cannot read property signInWithEmailAndPassword of null

代码片段没有显示<link rel="import" ...>,但在我的代码中,我确实包含了这些行以及其他测试用例,<paper-input>并且<paper-button>正在通过。

我做错了什么?

4

1 回答 1

1

我不确定以下是否是我自己问题的答案,但是在添加 astub返回 a之后Promise,错误消失了,上面的测试通过了。但是,我仍然没有弄清楚上面未定义错误的原因。

stub('firebase-auth', {
  signInWithEmailAndPassword: function (e, p) {
    return new Promise( (resolve, reject) => {
      resolve("Yes");
    });
  }
});
于 2017-03-11T00:15:29.820 回答