如何使用mockfirebase进行angular2打字稿单元测试?

时间:2015-12-04 09:33:52

标签: javascript unit-testing typescript firebase angular

如何使用mockfirebase模拟firebase进行firebase单元测试?

我正在使用typescript和angular2进行单元测试。

以下是我希望从firebase更改为mockfirebase的测试之一:

 import { it, iit, describe, expect, inject, injectAsync, beforeEachProviders, fakeAsync, tick } from 'angular2/testing';
 import { FirebaseService } from '../app/firebase-service';

 describe('Firebase Service Calls', () => {

      it('log out user from Firebase', () => {
          let ref = new Firebase("https://markng2.firebaseio.com");
          let service = new FirebaseService();

          spyOn(service.ref, 'unauth');
          service.logOut();

          expect(service.ref.unauth).toHaveBeenCalled();
      })

 });

我想更改此行:“让ref = new Firebase(”https://markng2.firebaseio.com“);”

1 个答案:

答案 0 :(得分:1)

Firebase类/函数一样,MockFirebase上提供了window

您可以在MockFirebase.override()功能或灯具中使用beforeEach

describe('Firebase Service Calls', () => {

      beforeEach(() => MockFirebase.override());

      it('log out user from Firebase', () => {
          let ref = new Firebase("https://markng2.firebaseio.com");
          ref.unauth();
          ref.flush(); // flush out the async action
          expect(ref.getUser()).toEqual(null);
      });
 });

当用户通过身份验证时,呼叫ref.getUser()将返回当前用户。上面的测试检查用户注销时ref.getUser()的返回值是否为空。

Check out the documentation on authentication and MockFirebase.