【问题标题】:angular testing Error: Can't resolve all parameters for WelcomeComponent: (?)角度测试错误:无法解析 WelcomeComponent 的所有参数:(?)
【发布时间】:2017-05-23 09:26:58
【问题描述】:

我把官网复制到我的项目下面,但是运行karma test的时候出现这个错误。 有谁知道这是为什么? 错误:无法解析 WelcomeComponent 的所有参数:(?)。 在 CompileMetadataResolver._getDependenciesMetadata (karma-shim.js:16768:19) [ProxyZone]

//welcome.component.ts
import { Component, OnInit } from '@angular/core';
import {UserService} from "./user.service";


@Component({
  selector: 'app-welcome',
  template: '<h3 class="welcome" ><i>{{welcome}}</i></h3>'
})
export class WelcomeComponent  implements OnInit {
  welcome = '-- not initialized yet --';
  constructor(private userService: UserService) { }

  ngOnInit(): void {
    this.welcome = this.userService.isLoggedIn ?
      'Welcome, ' + this.userService.user.name :
      'Please log in.';
  }
}

//welcome.component.spec.ts
describe('WelcomeComponent', () => {

  let comp: WelcomeComponent;
  let fixture: ComponentFixture<WelcomeComponent>;
  let componentUserService: UserService; // the actually injected service
  let userService: UserService; // the TestBed injected service
  let de: DebugElement;  // the DebugElement with the welcome message
  let el: HTMLElement; // the DOM element with the welcome message

  let userServiceStub: {
    isLoggedIn: boolean;
    user: { name: string}
  };

  beforeEach(() => {
    // stub UserService for test purposes
    userServiceStub = {
      isLoggedIn: true,
      user: { name: 'Test User'}
    };

    TestBed.configureTestingModule({
       declarations: [ WelcomeComponent ],
       providers:    [ {provide: UserService, useValue: userServiceStub } ]
    });

    fixture = TestBed.createComponent(WelcomeComponent);
    comp    = fixture.componentInstance;

    // UserService actually injected into the component
    userService = fixture.debugElement.injector.get(UserService);
    componentUserService = userService;
    // UserService from the root injector
    userService = TestBed.get(UserService);

    //  get the "welcome" element by CSS selector (e.g., by class name)
    de = fixture.debugElement.query(By.css('.welcome'));
    el = de.nativeElement;
  });

  it('should welcome the user', () => {
    fixture.detectChanges();
    const content = el.textContent;
    expect(content).toContain('Welcome', '"Welcome ..."');
    expect(content).toContain('Test User', 'expected name');
  });

  it('should welcome "Bubba"', () => {
    userService.user.name = 'Bubba'; // welcome message hasn't been shown yet
    fixture.detectChanges();
    expect(el.textContent).toContain('Bubba');
  });

  it('should request login if not logged in', () => {
    userService.isLoggedIn = false; // welcome message hasn't been shown yet
    fixture.detectChanges();
    const content = el.textContent;
    expect(content).not.toContain('Welcome', 'not welcomed');
    expect(content).toMatch(/log in/i, '"log in"');
  });

  it('should inject the component\'s UserService instance',
    inject([UserService], (service: UserService) => {
    expect(service).toBe(componentUserService);
  }));

  it('TestBed and Component UserService should be the same', () => {
    expect(userService === componentUserService).toBe(true);
  });

  it('stub object and injected UserService should not be the same', () => {
    expect(userServiceStub === userService).toBe(false);

    // Changing the stub object has no effect on the injected service
    userServiceStub.isLoggedIn = false;
    expect(userService.isLoggedIn).toBe(true);
  });
});

【问题讨论】:

  • 您能否添加您的测试模块配置?看起来您在测试模块提供程序中缺少 UserService。
  • 从 '@angular/core/testing' 导入 { ComponentFixture, inject, TestBed };从'@angular/platform-b​​rowser'导入{ By };从“@angular/core”导入 { DebugElement };从'./welcome.component'导入{ WelcomeComponent};从“./user.service”导入{UserService};
  • 你的意思是 test.module.ts
  • @W.Wei 你解决过这个问题吗?我也在尝试关注angular.io/guide/testing#component-with-a-dependency,但我得到了同样的错误。

标签: angular testing jasmine karma-runner


【解决方案1】:

我的项目中也有同样的错误,但对我来说,原因是由于循环依赖而无法注入一项服务 - 解决循环依赖为我解决了注入错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    相关资源
    最近更新 更多