I'm having problems with unit testing in angular 6. I'm trying to test a simple component with one input() parameter. The problem was that I don't know how to continue. I receive an error when executing ng test :
TypeError: Cannot read property 'id_profile' of undefined
My code is this:
Model
export class Profile {
id_profile: string;
name: string;
}
html
<div class="card" (click)="clicked()">
<div id="profile">{{profile.id_profile}}</div>
<i class="material-icons">vpn_key</i>
</div>
component
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Router } from '@angular/router';
import { Profile } from 'app/models';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.scss']
})
export class ProfileComponent {
@Input() profile: Profile;
private profileShown: Profile;
constructor(public router: Router) {
}
OnInit() {
this.profileShown= this.profile;
}
clicked() {
this.profileShown= this.profile;
this.router.navigate(['pages']);
}
}
And finally, my test :
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { ProfileComponent} from './profile.component';
describe('ProfileComponent', () => {
let component: ProfileComponent;
let fixture: ComponentFixture<ProfileComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [ProfileComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ProfileComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
component.profile= {
'id_profile': '12345678A',
'name': 'SUMUM_D',
};
fixture.detectChanges();
expect(component).toBeTruthy();
});
});
What I have to do to solve this problem? Am I doing the correct things? Thanks in advance