单元测试Angular与Jasmine和Karma,错误:无法绑定到'xxx',因为它不是'xxxxxx'的已知属性.

bul*_*msr 26 karma-jasmine angular

我对使用Jasmine和Karma的5角单元测试存在问题.

错误是:"无法绑定到'fruits',因为它不是'my-component2'的已知属性".

单元测试代码是:

SRC /应用/组件/我-COMPONENT1 /我-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();
  });
});
Run Code Online (Sandbox Code Playgroud)

这些是我的组件的代码:

SRC /应用/组件/我-COMPONENT1 /我-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);
  }
}
Run Code Online (Sandbox Code Playgroud)

SRC /应用/组件/我-COMPONENT1 /我-component1.component.html:

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

    <my-component2 [fruits]=fruits></my-component2>
Run Code Online (Sandbox Code Playgroud)

SRC /应用/组件/我-COMPONENT2 /我-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() {
  }

}
Run Code Online (Sandbox Code Playgroud)

SRC /应用/组件/我-COMPONENT2 /我-component2.component.html:

<p>
  my-component2 works!
</p>
Run Code Online (Sandbox Code Playgroud)

这是一个虚拟项目,任何人都可以帮助我吗?

谢谢 :)

nic*_*nli 31

您需要在创建测试模块时添加第二个组件,因为该模块是组件1的一部分.如果不是,模块将不具有my-component2,输入将无效.

TestBed.configureTestingModule({
      declarations: [ MyComponent1Component, MyComponent2Component ],
      imports: [],
      providers: [MyService1Service, HttpClient, HttpHandler, HttpTestingController]
    })
Run Code Online (Sandbox Code Playgroud)


Aja*_*ddy 6

对于某些人来说这可能会有所帮助 - 有时它可能只是可能丢失的特定模块

TestBed.configureTestingModule(
{
declarations: [TestComponent],
imports: [<Specific_module>],
}

Run Code Online (Sandbox Code Playgroud)