我试图通过将服务添加到规范中的providers数组并使用其功能添加createSpyObj来模拟服务,但出现以下错误:
从来源'http:localhost:9876'在ng://DynamicTestModule/mycomponent_Host.ngfactory.js'访问XMLHttpRequest我究竟做错了什么?// ....导入服务
mockService = jasmine.createSpyObj(['function1'])
testBed.configureTestingModule({
providers:[{
{provide: myService, useValue: mockService}
}]
}).compileComponents()
SharedService
的服务要测试// import the service from the desired location, i just indicated with @
import { SharedService } from "@services"
// create an instance inside describe
let sharedService: SharedService
// declare under providers in beforeEach
providers: [SharedService]
// create component and test fixture
fixture = TestBed.createComponent(yourComponentName);
component = fixture.componentInstance;
sharedService = fixture.debugElement.injector.get(SharedService)
// use compileComponents only if you are not using webpack or angular CLI
it('Should call MethodOne - sharedService ==> resolved', () => {
let MockedData = {};
spyOn(sharedService, 'someMethod').and.returnValue(Observable.of(MockedData));
component.MethodOne();
expect(sharedService.someMethod).toHaveBeenCalled();
});
HttpClientTestingModule