How to mock AngularFire 2 service in unit test?

Javier Villanueva

I'm trying to set up unit tests for a sample Angular 2 app using AngularFire 2 auth, the component is fairly simple:

import { Component } from '@angular/core';
import { AngularFire, AuthProviders } from 'angularfire2';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
export class AppComponent {
  isLoggedIn: boolean;

  constructor(public af: AngularFire) {
    this.af.auth.subscribe(auth => {
      if (auth) {
        this.isLoggedIn = true;
      } else {
        this.isLoggedIn = false;
      }
    });
  }

  loginWithFacebook() {
    this.af.auth.login({
      provider: AuthProviders.Facebook
    });
  }

  logout() {
    this.af.auth.logout();
  }
}

All I'm doing is wrapping around the login and logout methods in AngularFire so I was thinking about using a mock to check if the methods were called but I'm not sure where to start, I tried doing the following in my spec file:

import { provide } from '@angular/core';
import { AngularFire } from 'angularfire2';
import {
  beforeEach, beforeEachProviders,
  describe, xdescribe,
  expect, it, xit,
  async, inject
} from '@angular/core/testing';
import { AppComponent } from './app.component';

spyOn(AngularFire, 'auth');

beforeEachProviders(() => [
  AppComponent,
  AngularFire
]);

describe('App Component', () => {
  it('should create the app',
    inject([AppComponent], (app: AppComponent) => {
      expect(app).toBeTruthy();
    })
  );

  it('should log user in',
    inject([AppComponent], (app: AppComponent) => {
      expect(app.fb.auth.login).toHaveBeenCalled();
    })
  );

  it('should log user out',
    inject([AppComponent], (app: AppComponent) => {
      expect(app.fb.auth.logout).toHaveBeenCalled();
    })
  );
});

However I'm not sure how to mock the login and logout methods since they're part of the auth property, is there a way to mock auth and also the returning login and logout methods?

acdcjunior

In this snippet:

beforeEach(() => addProviders([
  AppComponent,
  AngularFire
]);

You set (or override) the providers that will be used in your test.

That being said, you can create a different class, a mock if you will, and, using the { provide: originalClass, useClass: fakeClass } notation, provide it instead of the AngularFire actual class.

Something like this:

class AngularFireAuthMock extends AngularFireAuth {           // added this class
  public login() { ... }
  public logout() { ... }
}

class AngularFireMock extends AngularFire {                   // added this class
  public auth: AngularFireAuthMock;
}

beforeEach(() => addProviders([
  AppComponent,
  { provide: AngularFire, useClass: AngularFireMock }         // changed this line
]);

And the AngularFires in your tests will be AngularFireMocks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

how to mock $window to unit test AngularJS service?

From Dev

How to mock Symfony 2 service in a functional test?

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

Grails Spock Mock Injected Service in Unit Test

From Dev

Angular Karma Unit Test: How to inject a mock service in karma configuration instead of in all test specs

From Dev

Angular2/testing: how to run test with mock service provider?

From Dev

Unit Test Web Api 2 Mock User

From Dev

Mock controller in Symfony2 unit test

From Dev

Mock controller in Symfony2 unit test

From Java

How do I mock a service that returns promise in AngularJS Jasmine unit test?

From Dev

How do you mock classes that are used in a service that you're trying to unit test using JUnit + Mockito

From Dev

Spring Integration Java DSL unit test - How to mock a Service Activator class or other components/endpoints?

From Dev

How to unit test $mdSidenav service?

From Dev

How to android unit test and mock a static method

From Dev

How to mock the UserAgent property for an HttpRequest in a unit test?

From Dev

How to unit test this function without mock asserts?

From Dev

How to mock browserHistory in unit test environment?

From Dev

How to mock multiple components in camel unit test?

From Dev

How to mock an Akka Actor to Unit Test a class?

From Dev

How to mock a generic parameter for a unit test in Java?

From Dev

How to mock a FormBuilder for Angular Component Unit Test

From Dev

How to mock multiple components in camel unit test?

From Dev

How to mock the UserAgent property for an HttpRequest in a unit test?

From Dev

How to define Mock Cursor for Android Unit Test

From Dev

Mock a controller for unit test without any service reference C#

From Dev

Test Angular2 service with mock backend

Related Related

  1. 1

    How to mock AngularFire 2 service in unit test?

  2. 2

    how to mock $window to unit test AngularJS service?

  3. 3

    How to mock Symfony 2 service in a functional test?

  4. 4

    How to mock springSecurityService in an unit test

  5. 5

    How to mock this unit test in Python?

  6. 6

    Correct way to mock an AngularJS Service in a unit test?

  7. 7

    Grails Spock Mock Injected Service in Unit Test

  8. 8

    Angular Karma Unit Test: How to inject a mock service in karma configuration instead of in all test specs

  9. 9

    Angular2/testing: how to run test with mock service provider?

  10. 10

    Unit Test Web Api 2 Mock User

  11. 11

    Mock controller in Symfony2 unit test

  12. 12

    Mock controller in Symfony2 unit test

  13. 13

    How do I mock a service that returns promise in AngularJS Jasmine unit test?

  14. 14

    How do you mock classes that are used in a service that you're trying to unit test using JUnit + Mockito

  15. 15

    Spring Integration Java DSL unit test - How to mock a Service Activator class or other components/endpoints?

  16. 16

    How to unit test $mdSidenav service?

  17. 17

    How to android unit test and mock a static method

  18. 18

    How to mock the UserAgent property for an HttpRequest in a unit test?

  19. 19

    How to unit test this function without mock asserts?

  20. 20

    How to mock browserHistory in unit test environment?

  21. 21

    How to mock multiple components in camel unit test?

  22. 22

    How to mock an Akka Actor to Unit Test a class?

  23. 23

    How to mock a generic parameter for a unit test in Java?

  24. 24

    How to mock a FormBuilder for Angular Component Unit Test

  25. 25

    How to mock multiple components in camel unit test?

  26. 26

    How to mock the UserAgent property for an HttpRequest in a unit test?

  27. 27

    How to define Mock Cursor for Android Unit Test

  28. 28

    Mock a controller for unit test without any service reference C#

  29. 29

    Test Angular2 service with mock backend

HotTag

Archive