我有一个测试组件和一些需要观众使用不同模拟的测试。问题是我无法让观众在测试进行时动态更改它们。
基本上我有 2 个从 .json 文件导入的模拟,一个是正确的,另一个是有错误的。
正确:profitability-customer.json 无效:profitability-customer-JSON-error-1.json
在测试 8 中,我需要观众将模拟切换为无效的。
这是我所拥有的:
import * as reportMock from '@app/__mocks__/reports/profitability-customer.json';
import * as reportMockError from '@app/__mocks__/reports/profitability-customer-JSON-error-1.json';
let spectator: Spectator<ProfitabilityCustomerReportComponent>;
beforeEach(() => {
spectator = createComponent();
spectator.component.reportForm.controls.brands.setValue([testCustomer.brands[1].bkId]);
testIter++;
});
createComponent = createComponentFactory({
component: ProfitabilityCustomerReportComponent,
imports: [SharedModule, HttpClientTestingModule, RouterTestingModule],
providers: [MapService, DateSelectService,
mockProvider(HeaderCustomerService, HeaderCustomerServiceMock),
mockProvider(MapService, MapServiceMock),
mockProvider(ReportingApiService, myFOO())
],
shallow: false
});
function myFOO() {
let ReportingApiServiceMock: any;
console.log(testIter);
if (testIter === 8) {
ReportingApiServiceMock = {
getReport() {
return of(reportMockError['profitability-customer-JSON-error-1']);
}
};
} else {
ReportingApiServiceMock = {
getReport() {
return of(reportMock['profitability-customer']);
}
};
}
return ReportingApiServiceMock;
};