我正在尝试使用模拟服务测试Angular 6组件。
我对下面代码的意图是使用嵌套的describe
语句来允许不同版本的模拟服务返回不同的结果
真实服务执行一些HTTP调用,而我的mock返回带有数据的Observable。
当我执行测试时,我看到Jasmine正在尝试实际执行HTTP调用而忽略了我的模拟
// Core imports
import { async, ComponentFixture, TestBed, inject, fakeAsync } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { DebugElement, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { ActivatedRoute, Params } from "@angular/router";
import { Observable } from "rxjs/Observable"
import 'rxjs/add/observable/of';
import { HttpClientModule } from "@angular/common/http";
// Vendor imports
import { ToastrModule, ToastrService } from 'ngx-toastr';
import { NgbModule } from "@ng-bootstrap/ng-bootstrap";
// Project imports
import { FormComponent } from './form.component';
import { FormService } from "../../services/form.service";
import { DatastoreService } from "../../services/datastore.service";
describe('FormComponent', () => {
let component: FormComponent;
let fixture: ComponentFixture<FormComponent>;
let de: DebugElement;
let el;
beforeEach(async(() => {
console.info('First setup of module');
return TestBed.configureTestingModule({
imports: [HttpClientModule, FormsModule, ToastrModule.forRoot(), NgbModule.forRoot()],
providers: [
FormService,
ToastrService,
DatastoreService,
{
provide: ActivatedRoute,
useValue: {
params: {
subscribe: (fn: (value: Params) => void) => fn({
tab: 0,
}),
},
snapshot: {
url: [{path: 'worker'}],
queryParams: {table: 'portal', sysId: undefined}
},
},
},
],
declarations: [ FormComponent ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
});
}));
beforeEach(() => {
console.info('establishing components');
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
de = fixture.debugElement;
el = de.nativeElement;
});
describe('with a form layout containing an invalid element', () => {
let FormServiceMock = class FormServiceMock {
constructor() { console.info('instantiated'); }
public getForm(table) {
console.log('getForm called');
return Observable.of({
"table": "portalx",
"schema": {
"name": {"title": "name", "element_type": "string"},
"suffix": {"title": "suffix", "element_type": "string"},
"active": {"title": "active", "element_type": "boolean"}
}, "layout": ["name", "suffix", "notexists", "active"]
});
}
};
beforeEach( () => {
console.info('Second re-setup of module');
TestBed.resetTestingModule();
return TestBed.configureTestingModule({
providers: [{ provide: FormService, useClass: FormServiceMock }]
}).compileComponents();
});
fit('ngOnInit sets values correctly', inject([FormService], (service: FormService) => {
console.info('hello world');
component.ngOnInit();
expect(component.portal).toEqual('worker');
}));
});
});
运行茉莉花测试给了我
Chrome 66.0.3359(Mac OS X 10.12.6)错误 Chrome 66.0.3359(Mac OS X 10.12.6)ERROR Disconnectedundefined Chrome 66.0.3359(Mac OS X 10.12.6):执行了37(跳过36)成功(1.779秒/ 1.655秒) sNm6eZUIrnZxAOwqAAAI,ID为92987428 Chrome 66.0.3359(Mac OS X 10.12.6):执行0 37 of 37 SUCCESS(0秒/ 0秒) 信息:&#39;首先设置模块&#39; Chrome 66.0.3359(Mac OS X 10.12.6):执行0 37 of 37 SUCCESS(0秒/ 0秒) 信息:&#39;首先设置模块&#39; 27 05 2018 17:36:00.293:WARN [web-server]:404:/ api / services / ui / form / portal 27 05 2018 17:36:00.551:WARN [web-server]:404:/ api / services / ui / form / portal 信息:&#39;建立组件&#39; Chrome 66.0.3359(Mac OS X 10.12.6):执行0 37 of 37 SUCCESS(0秒/ 0秒) 信息:&#39;建立组件&#39; 信息:&#39;第二次重新设置模块&#39; Chrome 66.0.3359(Mac OS X 10.12.6):执行0 37 of 37 SUCCESS(0秒/ 0秒) 信息:&#39;第二次重新设置模块&#39; 信息:&#39;实例化&#39; Chrome 66.0.3359(Mac OS X 10.12.6):执行0 37 of 37 SUCCESS(0秒/ 0秒) 信息:&#39;实例化&#39; 信息:&#39;你好世界&#39; Chrome 66.0.3359(Mac OS X 10.12.6):执行0 37 of 37 SUCCESS(0秒/ 0秒) 信息:&#39;你好世界&#39; 日志:HttpErrorResponse {headers:HttpHeaders {normalizedNames:Map {},lazyUpdate:null,lazyInit:function(){...}},状态:404, statusText:&#39; Not Found&#39;,url:&#39; http://localhost:9876/api/seLOG: HttpErrorResponse {headers:HttpHeaders {normalizedNames:Map {}, lazyUpdate:null,lazyInit:function(){...}},状态:404, statusText:&#39; Not Found&#39;,url: &#39; http://localhost:9876/api/services/ui/form/portal&#39;,ok:false,name: &#39; HttpErrorResponse&#39;,消息:&#39; Http失败响应 http://localhost:9876/api/services/ui/form/portal:404 Not Found&#39;, 错误:&#39;未找到&#39;} Chrome 66.0.3359(Mac OS X 10.12.6):执行0 37 of 37 SUCCESS(0秒/ 0秒) 日志:HttpErrorResponse {headers:HttpHeaders {normalizedNames:Map {},lazyUpdate:null,lazyInit:function(){...}},状态:404, statusText:&#39; Not Found&#39;,url: &#39; http://localhost:9876/api/services/ui/form/portal&#39;,ok:false,name: &#39; HttpErrorResponse&#39;,消息:&#39; Http失败响应 http://localhost:9876/api/services/ui/form/portal:404 Not Found&#39;, 错误:&#39;未找到&#39;} 日志:HttpErrorResponse {headers:HttpHeaders {normalizedNames:Map {},lazyUpdate:null,lazyInit:function(){...}},状态:404, statusText:&#39; Not Found&#39;,url:&#39; http://localhost:9876/api/seLOG: HttpErrorResponse {headers:HttpHeaders {normalizedNames:Map {}, lazyUpdate:null,lazyInit:function(){...}},状态:404, statusText:&#39; Not Found&#39;,url: &#39; http://localhost:9876/api/services/ui/form/portal&#39;,ok:false,name: &#39; HttpErrorResponse&#39;,消息:&#39; Http失败响应 http://localhost:9876/api/services/ui/form/portal:404 Not Found&#39;, 错误:&#39;未找到&#39;} Chrome 66.0.3359(Mac OS X 10.12.6):执行0 37 of 37 SUCCESS(0秒/ 0秒) 日志:HttpErrorResponse {headers:HttpHeaders {normalizedNames:Map {},lazyUpdate:null,lazyInit:function(){...}},状态:404, statusText:&#39; Not Found&#39;,url:&#39; http://localhost:9876/api/seChrome 66.0.3359(Mac OS X 10.12.6):执行第37条(跳过36条)成功(1.638秒/1.52秒)
在该日志输出中,我看到Instantiated
在我的模拟构造函数中,但我没有看到调用我的getForm
方法,而是从服务调用真实的方法。
Chrome 66.0.3359(Mac OS X 10.12.6)错误Disconnectedundefined 27 05 2018 17:35:58.128:
INFO [Chrome 66.0.3359 (Mac OS X 10.12.6)]: Connected on socket
对此有何帮助?