模拟角业茉莉花测试中的服务

问题描述 投票:0回答:2

我试图通过将服务添加到规范中的providers数组并使用其功能添加createSpyObj来模拟服务,但出现以下错误:

从来源'http:localhost:9876'在ng://DynamicTestModule/mycomponent_Host.ngfactory.js'访问XMLHttpRequest我究竟做错了什么?// ....导入服务

mockService = jasmine.createSpyObj(['function1'])
testBed.configureTestingModule({
  providers:[{
      {provide: myService, useValue: mockService}
  }]
}).compileComponents()
angular jasmine karma-jasmine
2个回答
0
投票
仅说您有一个名为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(); });


0
投票
我要做的是提供服务,但要利用Angular的

HttpClientTestingModule

© www.soinside.com 2019 - 2024. All rights reserved.