Angular `错误:无法执行';发送';在';XMLHttpRequest';`在一个角度的业力测试中
我有一个角度组件和相应的规范文件,用于Karma测试。我用于UI组件,在本例中,它是一个单选按钮组件。问题是,我有两个单选按钮输入,一个是使用组件本身的静态值渲染的,另一个是使用来自服务器的数据渲染的,这是在测试中模拟的。以下是我的组件的外观:Angular `错误:无法执行';发送';在';XMLHttpRequest';`在一个角度的业力测试中,angular,testing,jasmine,karma-jasmine,primeng,Angular,Testing,Jasmine,Karma Jasmine,Primeng,我有一个角度组件和相应的规范文件,用于Karma测试。我用于UI组件,在本例中,它是一个单选按钮组件。问题是,我有两个单选按钮输入,一个是使用组件本身的静态值渲染的,另一个是使用来自服务器的数据渲染的,这是在测试中模拟的。以下是我的组件的外观: @Component({ template: ` <div *ngFor="let unitRange of unitRanges"> <p-radioButton [value]="unitRange.value" [label]="
@Component({
template: `
<div *ngFor="let unitRange of unitRanges">
<p-radioButton [value]="unitRange.value" [label]="unitRange.label" formControlName="unitRange" name="unitRange"></p-radioButton>
<div *ngFor="let offerType of offerTypes">
<p-radioButton [value]="offerType.value" [label]="offerType.label" formControlName="negotiationTypeID" name="negotiationTypeID"></p-radioButton>
</div>
</div>
`
})
export class SearchCriteriaComponent {
form: FormGroup;
public offerTypes: SelectItem[] = [{value: 1, label: 'Sell'}, {value: 2, label: 'Buy'}];
public unitRanges: SelectItem[] = [];
constructor(protected fb: FormBuilder, protected searchService: SearchService) {
this.form = fb.group({
'unitRange': ['']
'negotiationTypeID': [this.offerTypes[0].value]
});
}
ngOnInit() {
this.searchService.getOfferSearchLookups().then(response => this.unitRanges = response)
}
}
@组件({
模板:`
`
})
导出类SearchCriteria组件{
表格:表格组;
公开发售类型:SelectItem[]=[{value:1,标签:'Sell'},{value:2,标签:'Buy'}];
公共单位范围:SelectItem[]=[];
构造函数(受保护的fb:FormBuilder,受保护的searchService:searchService){
this.form=fb.group({
“单位范围”:[“”]
“negotiationTypeID:[this.offerTypes[0].value]
});
}
恩戈尼尼特(){
this.searchService.getOfferSearchLookups().then(response=>this.unitRanges=response)
}
}
这是我的测试:
const unitRanges = [{value: 1, label: '1 unit'}, {value: 2, label: '2 unit'}, {value: 1, label: '3 unit'}],
export const searchServiceStub = {
getOfferSearchLookups() {
return Promise.resolve(unitRanges);
}
} // mock the service
describe('SearchCriteriaComponent', () => {
let component: SearchCriteriaComponent;
let fixture: ComponentFixture<SearchCriteriaComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SearchCriteriaComponent ],
imports: [
CommonModule,
ReactiveFormsModule,
MultiSelectModule,
RadioButtonModule,
NoopAnimationsModule // used for mocking animations for Primeng components
],
providers: [
{provide: CommodityService, useValue: commoditiesServiceStub},
{provide: SearchService, useValue: searchServiceStub}
]
})
.compileComponents(); }));
});
beforeEach(() => {
fixture = TestBed.createComponent(SearchCriteriaComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be created', () => {
expect(component).toBeTruthy();
});
it('should load the initial radiobuttons', async(() => {
fixture.detectChanges(); // test fails here, without any expectation
}));
});
const unitRanges=[{value:1,标签:'1 unit'},{value:2,标签:'2 unit'},{value:1,标签:'3 unit'}],
导出常量searchServiceStub={
getOfferSearchLookups(){
返回承诺。解决(unitRanges);
}
}//模拟服务
描述('SearchCriteriaComponent',()=>{
let组件:SearchCriteria组件;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[SearchCriteriaComponent],
进口:[
公共模块,
反应形式模块,
多选模块,
RadioButtonModule,
NoopAnimationsModule//用于模拟组件的动画
],
供应商:[
{提供:CommodityService,使用值:CommodityServiceStub},
{提供:SearchService,useValue:searchServiceStub}
]
})
.compileComponents();}));
});
在每个之前(()=>{
fixture=TestBed.createComponent(SearchCriteriaComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
它('应该加载初始单选按钮'),异步(()=>{
fixture.detectChanges();//测试在此失败,没有任何预期
}));
});
(为了简洁起见,我省略了th import语句。此外,该模板不是内联的,并且驻留在单独的HTML文件中,我只是将其放在这里,以便于阅读和理解)。
当我注释掉模板中的第一个radiobutton组件时,测试将持续运行。但是,当我使用第一个单选按钮时,它会给我一个错误
失败:未能在“XMLHttpRequest”上执行“send”:未能加载“ng:///RadioButtonModule/RadioButton.ngfactory.js”。
。因此,对于使用来自组件的本地数据呈现的单选按钮,测试可以正常工作,但对于异步数据,测试失败。这是什么意思,如何修复?您解决过这个问题吗?我有一个类似的问题,我也对解决这个问题感兴趣!如果遇到此问题,请共享:)在每个(()=>{fixture=TestBed.createComponent(AgendaDetailComponent);component=fixture.componentInstance;//fixture.detectChanges();})之前;注释此fixture.detectChanges();只是让测试正常:/I我只是不知道如何解决这个问题。