这看起来很简单,我知道我肯定做错了什么,但我已经做了一整天了,仍然无法通过一个简单的测试。我的组件使用第三方库JQWidgets,部分问题是测试报告了该库中的错误,但测试失败的另一部分是,显然我应该加载到组件中属性的模拟数据没有进入其中。
我试着用玩笑式的嘲弄,但是尽管我已经把文档读了十几遍了,我还是不确定我做得对不对。有时我需要一个间谍,有时我只希望模拟函数(通常是服务函数)返回一些伪数据。我觉得我只是不懂一些戏谑的魔术。
编辑:模拟整个服务
这看起来工作得更好,因为我的测试断言通过了,但是由于第三方模块中的错误,测试仍然失败。我尝试过添加“无错误”模式,但没有帮助:
TypeError: bA.getPropertyValue is not a function
at R (node_modules/jqwidgets-scripts/jqwidgets/jqxcore.js:14:36743)
ngOnInit() {
this._dataService.getData().subscribe(
data => {
this.populate(data); // <--this line appears not to be running, as this function populates this.source.localdata with this data
this.dataAdapter = new jqx.dataAdapter(this.source);
},
error => {
console.error(error);
}
);
}
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { jqxGridComponent } from 'jqwidgets-scripts/jqwidgets-ts/angular_jqxgrid';
import { ProgressComponent } from './progress.component';
import { ProgressGridDataService } from './progress.service';
import { WorkingFileService } from 'src/app/services/working-file/working-file.service';
import { IProgressData } from './progress.interfaces';
import { of } from 'rxjs';
describe('ProgressComponent', () => {
let component: ProgressComponent;
let fixture: ComponentFixture<ProgressComponent>;
class ProgressServiceSpy {
testHistory: IProgressData = {
recordId: 1,
product: 'A',
};
getData = jest.fn().mockImplementation(() => of(Object.assign({}, this.testHistory)));
restartLoad = jest.fn();
}
beforeEach(
async(() => {
TestBed.configureTestingModule({
declarations: [ProgressComponent, jqxGridComponent],
providers: [
WorkingFileService,
{ provide: ProgressGridDataService, useValue: {} },
],
})
.overrideComponent(ProgressComponent, {
set: {
providers: [
{ provide: ProgressGridDataService, useClass: ProgressServiceSpy },
],
},
})
.compileComponents();
fixture = TestBed.createComponent(ProgressComponent);
component = fixture.componentInstance;
fixture.detectChanges();
})
);
describe('retry button', () => {
it('should call the restartLoad service and pass a record ID', () => {
component.ngOnInit();
expect(component.source).toHaveProperty('localdata');
expect(component.source.localdata).toEqual(testHistory);
});
});
});