mercredi 22 mars 2017

Angular2 Testing Mock Service Issue

Hi guys is the first time that i test angular2 components.

I have this spec

import {TestBed, ComponentFixture} from '@angular/core/testing';
import { LoginComponent } from './login.component';
import {UserService} from "../services/user.service";
import {HttpModule} from "@angular/http";
import {FormsModule} from "@angular/forms";
import {RouterTestingModule} from "@angular/router/testing";

describe('LoginComponent', () => {

  let component: LoginComponent;
  let service: UserService;
  let fixture: ComponentFixture<LoginComponent>;



  class UserServiceStub {
    userLogin(email: string, password: string) {
      return [email, password];
    }
  }


  beforeEach(() => {

    TestBed.configureTestingModule({
      declarations: [LoginComponent],
      imports: [HttpModule, FormsModule, RouterTestingModule],
      providers: [
        {provide: UserService, useClass: UserServiceStub}
      ]
    });

    service = TestBed.get(UserService);
    fixture = TestBed.createComponent(LoginComponent);
    component = fixture.componentInstance;

  });





  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should call the service when the sign in button is clicked', () => {
    component.onLoginButtonClick();
    expect(service.userLogin("pippo", "pluto")).toBe(["pippo", "pluto"]);

  });
});

I can't figure out what is the displayed issue:

TypeError: this.userService.userLogin(...).catch is not a function
    at LoginComponent.onLoginButtonClick (http://ift.tt/2nSgiw7)
    at Object.<anonymous> (http://ift.tt/2nJUzJQ)
    at ZoneDelegate.invoke (http://ift.tt/2nS71E2)
    at ProxyZoneSpec.onInvoke (http://ift.tt/2nJILHt)
    at ZoneDelegate.invoke (http://ift.tt/2nSaWRp)
    at Zone.run (http://ift.tt/2nJVHNz)
    at Object.<anonymous> (http://ift.tt/2nSaUJh)
    at attemptSync (http://ift.tt/2nJVZnK)
    at ZoneQueueRunner.QueueRunner.run (http://ift.tt/2nStjFN)
    at ZoneQueueRunner.QueueRunner.execute (http://ift.tt/2nJUciB)
    at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (http://ift.tt/2nSh1gI)
    at Spec.queueRunnerFactory (http://ift.tt/2nJZyKD)
    at Spec.execute (http://ift.tt/2nS8oD1)
    at Object.fn (http://ift.tt/2nJYruh)
    at attemptAsync (http://ift.tt/2nSeD9N)

It looks like the test goes on the real service and not on the UserServiceStub.

Aucun commentaire:

Enregistrer un commentaire