angular - 错误:: 期待一个间谍,但在 Jamsine 中获得了功能
问题描述
我是使用 Jasmine 和 Karma 在 Angular 中进行单元测试的新手。我已经写了一个规范文件,但我得到了一个错误。让我先展示一下我到目前为止所做的事情。我创建了一个自定义组件:
时间选择器组件
startRange = moment('12-01-01');
endRange = moment ('12-12-01');
modes= ['Calendar Year', 'Year-to-date', 'Rolling Year', 'Custom'];
date = new Date();
currentYear = this.date.getFullYear(); // or simply currentYear = 2020;
@ViewChild('primaryMonthPicker') primaryMonthPicker: MonthpickerComponent; // a child component
primaryDropDown(startRange, endRange) {
if (this.primaryMode === this.modes[0]) {
this.initCalendarYear(this.primaryMonthPicker, this.currentYear);
} else if (this.primaryMode === this.modes[1]) {
this.initYearToDate(this.primaryMonthPicker, this.currentYear);
} else if (this.primaryMode === this.modes[2]) {
this.initRollingYear(this.primaryMonthPicker, this.currentYear);
}
}
在上面的代码MonthpickerComponent
中是另一个自定义组件,它是TimeselectorComponent
.
我只想检查何时primaryDropDown
调用然后initCalendarYear
也应该调用。这是规范文件:
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { TimeselectorComponent } from './timeselector.component';
...
describe('TimeselectorComponent', () => {
let fixture: ComponentFixture<TimeselectorComponent>;
@Component({
selector: 'app-monthpicker',
template: '<div></div>'
})
class FakeMonthpickerComponent {
// methods of MonthpickerComponent
}
let MODES = [];
beforeEach(async(() => {
MODES = ['Calendar Year', 'Year-to-date', 'Rolling Year', 'Custom'];
TestBed.configureTestingModule({
declarations: [FakeMonthpickerComponent, TimeselectorComponent],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TimeselectorComponent);
});
// other test cases that are passing
// need help with this test case
it('should call initCalendarYear when primaryDropDown is called', () => {
const startRange=moment('2020-01-01');
const endRange= moment('2020-12-01');
spyOn(fixture.componentInstance, 'initCalendarYear');
fixture.componentInstance.primaryMode=MODES[0];
fixture.componentInstance.primaryDropDown(startRange, endRange);
expect(fixture.componentInstance.initCalendarYear).toHaveBeenCalled();
});
});
我必须测试是否为关联if-else
语句调用了正确的方法。请帮我。
这是堆栈闪电战。
解决方案
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { TimeselectorComponent } from './timeselector.component';
import { Component, NO_ERRORS_SCHEMA } from '@angular/core';
var moment = require('moment/moment')
describe('TimeselectorComponent', () => {
let fixture: ComponentFixture<TimeselectorComponent>;
@Component({
selector: 'app-monthpicker',
template: '<div></div>'
})
class FakeMonthpickerComponent {
// methods of MonthpickerComponent
}
let MODES = [];
beforeEach(async(() => {
MODES = ['Calendar Year', 'Year-to-date', 'Rolling Year', 'Custom'];
TestBed.configureTestingModule({
declarations: [FakeMonthpickerComponent, TimeselectorComponent],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TimeselectorComponent);
});
// other test cases that are passing
// need help with this test case
it('should call initCalendarYear when primaryDropDown is called', () => {
const startRange=moment('2020-01-01');
const endRange= moment('2020-12-01');
spyOn(fixture.componentInstance, 'initCalendarYear');
fixture.componentInstance.primaryMode=MODES[0];
fixture.componentInstance.primaryDropDown(startRange, endRange);
expect(fixture.componentInstance.initCalendarYear).toHaveBeenCalled();
});
});
上面的游戏我成功了:-
推荐阅读
- java - 如何在 java 中将我的 .txt 文件转换为字符串?
- javascript - execCommand backColor 和 hilitecolor 在 chrome 上不起作用
- python - 如何设置 python 3 Telethon 连接超时?
- opencv - cv::imread 图像未显示在 MFC 的图片框上
- symfony - Symfony 4 - 自定义 KnpPaginator 分页模板?
- mongodb - MongoDB查找,限制跳过一些文档
- unit-testing - @SpringBootTest autowire 存储库无法将数据保存到真实数据库
- groovy - groovy 将字符串评估为存在于同一脚本中的函数
- php - Composer 更新在 composer install 工作时挂起
- c# - 检查字符串是否为空、是否包含 int 或是否为整数