lundi 10 avril 2017

How to access Angular2 components methods while testing?

So, I have Angular2 component that looks like this:

import {Component, OnInit} from '@angular/core';

@Component({
    selector: 'login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
    user = {
        username: '',
        password: ''
    };

    constructor() {
    }

    login() {
    }

    ngOnInit() {
    }

}

And I have test suite set up (only relevant part showed):

describe('LoginComponent', () => {
  let component: LoginComponent;
  let fixture: ComponentFixture<LoginComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ LoginComponent ],
      imports: [FormsModule]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(LoginComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });
it('should define method login()', () => {
    console.log(component);  //access data from component
    console.log(fixture.debugElement.nativeElement);  //access dom of component
  });

});

And I'm trying to write test for that login() function using Karma and Jasmine. But I simply can't access the login method. I've looked at a lot of sites, and most of them show examples with how to access properties, but never methods. Am I doing something wrong conceptually by trying to access class methods in test suite or something? (I'm not that good at testing)

Aucun commentaire:

Enregistrer un commentaire