我正在用 Jasmine 为 Angular 2 组件编写单元测试。当我的组件被实例化时,我想测试我的文档标题是否设置为特定值。
这是我的组件
import { Component } from '@angular/core';
import { Title } from '@angular/platform-browser';
@Component({
selector: 'cx-account',
templateUrl: 'app/account/account.component.html',
})
export class AccountComponent {
public constructor(private titleService: Title ) {
titleService.setTitle("Account");
}
}
这是我为测试而编写的,但它不起作用。titleService.getTitle()
给我 Karma 调试运行器页面标题。
import { TestBed } from '@angular/core/testing';
import { Title, By } from '@angular/platform-browser';
import { AccountComponent } from './account.component';
describe('AppComponent Tests', function () {
let titleService: Title = new Title();
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AccountComponent],
providers: [ {provide: Title } ],
});
let fixture = TestBed.createComponent(AccountComponent);
});
it('Title Should be Account', () => {
expect(titleService.getTitle()).toBe('Account');
});
});
业力输出是:
错误:预期 'Karma DEBUG RUNNER' 为 'Account'。