2

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

2 Answers 2

2

Do not run detectChanges before you set the data to your @Input() property, because it renders the view and throws error for uninitialized properties.

beforeEach(() => {
    fixture = TestBed.createComponent(ProfileComponent);
    component = fixture.componentInstance;
    // fixture.detectChanges();    -- Remove this line
});
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I've spent hours of my life looking for solutions and testing things. Many many thanks
0

Please let me know if it still shows error

 it('should create', () => {
    component.profileShown= {
      'id_profile': '12345678A',
      'name': 'SUMUM_D',      
    };
    fixture.detectChanges();
    expect(component).toBeTruthy();
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.