我正在尝试编写一个依赖于httpclient和Router的服务的测试用例。
我尝试了Karma + Jasmine: Cannot read property 'getComponentFromError'。 仍然出现相同的错误。也许还有其他问题。我在这里需要帮助
在运行该测试用例时,我会遇到一条错误消息:“失败:无法读取null的属性'getComponentFromError'”
我已经尝试过类似的解决方案
import {TestBed} from '@angular/core/testing';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {NavigationEnd, Router} from '@angular/router';
import {AddaService} from './adda.service';
import {RequestUrl} from './request.url';
import {countryList} from './countries';
describe('AddaService', () => {
let addaService: AddaService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RequestUrl, countryList, HttpClient, HttpHeaders, HttpParams, NavigationEnd, Router],
providers: [AddaService]
});
addaService = TestBed.get(AddaService);
});
it('should be createable', () => {
expect(addaService).toBeTruthy();
});
});
beforeEach(() => TestBed.configureTestingModule({
providers: [AddaService]
}));
it('should be created', inject([HttpTestingController, AddaService],
(httpMock: HttpTestingController, service: AddaService) => {
// We call the service
service.getcountries();
expect(service.countries).toBeTruthy();
})
);
期望结果应该是成功的。但我低于错误。
Failed: Cannot read property 'getComponentFromError' of null
TypeError: Cannot read property 'getComponentFromError' of null
at TestBedViewEngine._initIfNeeded (/Users/manoj_adda/packages/core/testing/src/test_bed.ts:393:46)
at TestBedViewEngine.get (/Users/manoj_adda/packages/core/testing/src/test_bed.ts:473:10)
at Function.TestBedViewEngine.get (/Users/manoj_adda/packages/core/testing/src/test_bed.ts:243:36)
at UserContext.<anonymous> (/Users/manoj_adda/Sites/addaio/src/app/adda.service.spec.ts:16:25)
at /Users/manoj_adda/Sites/addaio/node_modules/jasminewd2/index.js:112:25
at new ManagedPromise (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:1067:7)
at ControlFlow.promise (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2396:12)
at schedulerExecute (/Users/manoj_adda/Sites/addaio/node_modules/jasminewd2/index.js:95:18)
at TaskQueue.execute_ (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2970:14)
at TaskQueue.executeNext_ (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2953:27)
From: Task: Run beforeEach in control flow
at UserContext.<anonymous> (/Users/manoj_adda/Sites/addaio/node_modules/jasminewd2/index.js:94:19)
at attempt (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4486:26)
at QueueRunner.run (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4402:20)
at runNext (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4446:20)
at /Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4453:13
at /Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4356:12
at /Users/manoj_adda/Sites/addaio/node_modules/jasminewd2/index.js:64:48
at ControlFlow.emit (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/events.js:62:21)
at ControlFlow.shutdown_ (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2565:10)
at shutdownTask_.MicroTask (/Users/manoj_adda/Sites/addaio/node_modules/selenium-webdriver/lib/promise.js:2490:53)
From asynchronous test:
Error
at Suite.<anonymous> (/Users/manoj_adda/Sites/addaio/src/app/adda.service.spec.ts:11:2)
at addSpecsToSuite (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1181:25)
at Env.describe (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1145:7)
at describe (/Users/manoj_adda/Sites/addaio/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4593:18)
at Object.<anonymous> (/Users/manoj_adda/Sites/addaio/src/app/adda.service.spec.ts:9:1)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Module.m._compile (/Users/manoj_adda/Sites/addaio/node_modules/ts-node/src/index.ts:422:23)
at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/manoj_adda/Sites/addaio/node_modules/ts-node/src/index.ts:425:12)