Angular 角度单元测试-错误:<;已装入>;:预期是间谍,但得到了功能

Angular 角度单元测试-错误:<;已装入>;:预期是间谍,但得到了功能,angular,unit-testing,Angular,Unit Testing,你好,我正在写两个单元测试。对于第二个,我尝试使用下面的代码,但我得到了以下错误 错误::应该是间谍,但得到了函数。 我找到了在我的组件中编写的spy on方法的解决方案,但我找不到如何测试这个setTitle()方法!有什么想法吗 我的第一个组件 import { Component, OnInit } from '@angular/core'; import { Title } from '@angular/platform-browser'; @Component({ selecto

你好,我正在写两个单元测试。对于第二个,我尝试使用下面的代码,但我得到了以下错误 错误::应该是间谍,但得到了函数。

我找到了在我的组件中编写的spy on方法的解决方案,但我找不到如何测试这个setTitle()方法!有什么想法吗

我的第一个组件

import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';

@Component({
  selector: 'app-mission',
  template: '<p>{{caption}}</p>'
})
export class FirstComponent implements OnInit {

caption: string;

constructor(private title: Title) { }

ngOnInit() {
  this.title.setTitle('Mission accomplished');
 }
}
从'@angular/core'导入{Component,OnInit};
从“@angular/platform browser”导入{Title};
@组成部分({
选择器:“应用程序任务”,
模板:'{{caption}}

' }) 导出类FirstComponent实现OnInit{ 标题:字符串; 构造函数(私有名称:title){} 恩戈尼尼特(){ 本.title.setTitle(“任务完成”); } }
规范文件

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Title } from '@angular/platform-browser';

import { FirstComponent} from './mission.component';

describe('FirstComponent', () => {
  let component: FirstComponent;
  let fixture: ComponentFixture<FirstComponent>;
  let title: Title;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
     declarations: [ FirstComponent]
   });
fixture = TestBed.createComponent(FirstComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));

it('should create', () => {
  expect(component).toBeTruthy();
});

it('should set the caption property', () => {
});

it('should call the setTitle method', () => {
  title = TestBed.inject(Title);
  expect(title.setTitle).toHaveBeenCalled();
});

});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从“@angular/platform browser”导入{Title};
从“/mission.component”导入{FirstComponent};
描述('FirstComponent',()=>{
let组件:第一个组件;
let夹具:组件夹具;
让标题:标题;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[第一部分]
});
fixture=TestBed.createComponent(FirstComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
}));
它('应该创建',()=>{
expect(component.toBeTruthy();
});
它('应该设置标题属性',()=>{
});
它('应该调用setTitle方法',()=>{
title=TestBed.inject(title);
expect(title.setTitle).tohavebeincall();
});
});

你应该使用
spyOn

像这样:

此外,还必须提供依赖项。使用
TestBed.inject
是不够的

例如,将您的beforeach更改为:

  let titleSpyService : jasmine.SpyObj<Title>;
  beforeEach(() => {
    const titleSpy = jasmine.createSpyObj('Title', ['setTitle']);

    TestBed.configureTestingModule({
     declarations: [ FirstComponent],
     providers: [
       { provide: Title, useValue: titleSpy }
    ] 
   });
  fixture = TestBed.createComponent(FirstComponent);
  component = fixture.componentInstance;
  titleSpyService = TestBed.inject(Title) as jasmine.SpyObj<Title>;
 });
let titleSpyService:jasmine.SpyObj;
在每个之前(()=>{
const titleSpy=jasmine.createSpyObj('Title',['setTitle']);
TestBed.configureTestingModule({
声明:[FirstComponent],
供应商:[
{提供:Title,使用值:titleSpy}
] 
});
fixture=TestBed.createComponent(FirstComponent);
组件=fixture.componentInstance;
titleSpyService=TestBed.inject(Title)为jasmine.SpyObj;
});

我尝试过,但找不到setTitle方法!我使用其他方法(如ngOnInit)测试该组件上的实现,并且它正在工作。如何访问属于Title类的setTitle方法?仍然相同,找不到此方法!我创建了一个stackblitz,如果这样可以更容易地帮助更改title=TestBed.inject(title);totitle=TestBed.get(title);get已弃用:从v9.0.0开始使用TestBed.inject是相同的