Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test Angular with Jasmine and Karma, Error:Can't bind to 'xxx' since it isn't a known property of 'xxxxxx'.

I have a problem with an unit test in angular 5 with Jasmine and Karma.

The error is: "Can't bind to 'fruits' since it isn't a known property of 'my-component2'".

The unit test code is:

src/app/components/my-component1/my-component1.component.spec.ts:

import { TestBed, inject, ComponentFixture, async} from '@angular/core/testing';  import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';    import { Post } from '../../models/post.model';    import { MyComponent1Component } from './my-component1.component';  import { MyService1Service } from '../../services/my-service1.service';    import { HttpClientModule, HttpClient,HttpHandler } from '@angular/common/http';  import {fruit} from '../../Models/fruit';      fdescribe('MyComponent1Component', () => {    let component: MyComponent1Component;    let fixture: ComponentFixture<MyComponent1Component>;      beforeEach(async(() => {      TestBed.configureTestingModule({        declarations: [ MyComponent1Component ],        imports: [],        providers: [MyService1Service, HttpClient, HttpHandler, HttpTestingController]      })        .compileComponents();    }));      beforeEach(async(() => {      fixture = TestBed.createComponent(MyComponent1Component);      component = fixture.componentInstance;      fixture.detectChanges();    }));      it('should create', () => {      expect(component).toBeTruthy();    });  });

And these are the code of my components:

src/app/components/my-component1/my-component1.component.ts:

import { Component, OnInit } from '@angular/core';  import { MyService1Service} from '../../services/my-service1.service';  import {fruit} from '../../Models/fruit';    @Component({    selector: 'my-component1',    templateUrl: './my-component1.component.html',    styleUrls: ['./my-component1.component.css']  })  export class MyComponent1Component implements OnInit {      constructor(private _MyService1Service: MyService1Service) { }      public fruit= new fruit();      public fruits: fruit[];    ngOnInit() {      this._MyService1Service.getPost().subscribe(          result => {                console.log(result);          },          error => {            console.log(<any>error);          }        );        this.fruit.name = 'apple';      this.fruits.push(this.fruit);    }  }

src/app/components/my-component1/my-component1.component.html:

 <p>        my-component1 works!      </p>        <my-component2 [fruits]=fruits></my-component2>

src/app/components/my-component2/my-component2.component.ts:

import { Component, OnInit, Input } from '@angular/core';  import {fruit} from '../../Models/fruit';    @Component({    selector: 'my-component2',    templateUrl: './my-component2.component.html',    styleUrls: ['./my-component2.component.css']  })  export class MyComponent2Component implements OnInit {      @Input() fruits: fruit[];      constructor() { }      ngOnInit() {    }    }

src/app/components/my-component2/my-component2.component.html:

<p>    my-component2 works!  </p>

It is a dummy project, can anybody help me?

Thanks :)

like image 402
bulbasurmsr Avatar asked Mar 23 '18 11:03

bulbasurmsr


People also ask

How do you write and run tests in Angular using jasmine and karma?

Create an Angular project with jasmine and karma By doing this, the jasmine and karma configuration is resolved for us. Install and create a new project with angular-cli: Install npm -g @angular/cli. Ng new angular-unit-testing angular unit-testing.

What is the role of Jasmine and karma in Angular testing?

Jasmine is a behavior-driven development framework for testing JavaScript code that plays very well with Karma. Similar to Karma, it's also the recommended testing framework within the Angular documentation as it's setup for you with the Angular CLI. Jasmine is also dependency free and doesn't require a DOM.

What is Jasmine testing framework and how do you use it for Angular unit testing?

Jasmine is a behavior development testing framework. Unit tests are written using Jasmine and are run to see if individual parts of an application are working correctly. As a result, unit tests will either pass or fail depending on if the code is working correctly or has a bug.


1 Answers

You need to add your second component when creating your testing module as that module is part of component 1. If you don't the module won't have my-component2 and the input will be invalid.

TestBed.configureTestingModule({       declarations: [ MyComponent1Component, MyComponent2Component ],       imports: [],       providers: [MyService1Service, HttpClient, HttpHandler, HttpTestingController]     }) 
like image 188
nicowernli Avatar answered Sep 25 '22 02:09

nicowernli