我正在尝试在使用http:的Angular服务上运行Jasmine测试。
import { PostService } from './post.service';
import { Post } from '../_model/post';
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
describe('PostService', () => {
let httpTestingController: HttpTestingController;
let service: PostService;
beforeEach(() => {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
TestBed.configureTestingModule({
providers: [PostService],
imports: [HttpClientTestingModule]
});
httpTestingController = TestBed.get(HttpTestingController);
service = TestBed.get(PostService);
// setting up mock localStorage
let store = {};
const mockLocalStorage = {
getItem: (key: string): string => {
return key in store ? store[key] : null;
},
setItem: (key: string, value: string) => {
store[key] = `${value}`;
},
removeItem: (key: string) => {
delete store[key];
},
clear: () => {
store = {};
}
};
spyOn(localStorage, 'getItem')
.and.callFake(mockLocalStorage.getItem);
spyOn(localStorage, 'setItem')
.and.callFake(mockLocalStorage.setItem);
spyOn(localStorage, 'removeItem')
.and.callFake(mockLocalStorage.removeItem);
spyOn(localStorage, 'clear')
.and.callFake(mockLocalStorage.clear);
});
afterEach(() => {
httpTestingController.verify();
});
describe('getAll', () => {
it('should return an observable of Posts', () => {
const postData: Post[] = [
{id: '6aba8f8f-cd43-4b1c-b3a4-21aba97ab620', title: 'CSS Layouts: Justified Elements', created: '2018-3-20', content: 'css-layouts-justified-elements'},
{id: 'c49439a4-a24a-4e1b-bc92-ebad6caf5e74', title: 'The 4 Constraints of Project Management', created: '2018-02-05', content: 'the-4-constraints-of-project-management'},
{id: '23723e52-9f0d-4a5d-94e6-196382456258', title: 'Want a Project to Succeed? Gather Good Estimates', created: '2018-1-26', content: 'want-a-project-to-succeed-gather-good-estimates'},
{id: '69870c97-6736-4956-9826-8af4b2216a79', title: 'Measuring Technical Debt', created: '2019-7-7', content: 'measuring-technical-debt'},
{id: 'd05ee0b1-ade2-406c-a9ae-d22ba0229341', title: 'Estimations: Coping with Uncertainty', created: '2018-02-08', content: 'estimations-coping-with-uncertainty'}
];
service.getAll().subscribe(posts => {
expect(posts.length).toEqual(5);
});
const req = httpTestingController.expectOne('http://localhost:8089/posts');
expect(req.request.method).toEqual('GET');
req.flush(postData); // get the Observable to resolve 'flush' with the PostData
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
});
很多测试还没有完成,我只是想让事情正常进行。
基本上,测试几乎无法启动,并显示以下错误:
Failed: Can't resolve all parameters for ApplicationModule: (?).
Error: Can't resolve all parameters for ApplicationModule: (?).
at syntaxError (/Users/mikecoxon/dev/ws-other/packages/compiler/src/util.ts:100:17)
at CompileMetadataResolver._getDependenciesMetadata (/Users/mikecoxon/dev/ws-other/packages/compiler/src/metadata_resolver.ts:957:27)
at CompileMetadataResolver._getTypeMetadata (/Users/mikecoxon/dev/ws-other/packages/compiler/src/metadata_resolver.ts:836:20)
at CompileMetadataResolver.getNgModuleMetadata (/Users/mikecoxon/dev/ws-other/packages/compiler/src/metadata_resolver.ts:680:18)
at CompileMetadataResolver.getNgModuleSummary (/Users/mikecoxon/dev/ws-other/packages/compiler/src/metadata_resolver.ts:450:31)
at /Users/mikecoxon/dev/ws-other/packages/compiler/src/metadata_resolver.ts:586:44
at Array.forEach (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (/Users/mikecoxon/dev/ws-other/packages/compiler/src/metadata_resolver.ts:569:43)
at CompileMetadataResolver.getNgModuleSummary (/Users/mikecoxon/dev/ws-other/packages/compiler/src/metadata_resolver.ts:450:31)
at /Users/mikecoxon/dev/ws-other/packages/compiler/src/metadata_resolver.ts:586:44
From: Task: Run beforeEach in control flow
at UserContext.<anonymous> (/Users/mikecoxon/dev/ws-other/blog/node_modules/jasminewd2/index.js:94:19)
at attempt (/Users/mikecoxon/dev/ws-other/blog/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4297:26)
at QueueRunner.run (/Users/mikecoxon/dev/ws-other/blog/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4217:20)
at runNext (/Users/mikecoxon/dev/ws-other/blog/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4257:20)
at /Users/mikecoxon/dev/ws-other/blog/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4264:13
at /Users/mikecoxon/dev/ws-other/blog/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4172:9
at /Users/mikecoxon/dev/ws-other/blog/node_modules/jasminewd2/index.js:64:48
at ControlFlow.emit (/Users/mikecoxon/dev/ws-other/blog/node_modules/selenium-webdriver/lib/events.js:62:21)
at ControlFlow.shutdown_ (/Users/mikecoxon/dev/ws-other/blog/node_modules/selenium-webdriver/lib/promise.js:2674:10)
at shutdownTask_.MicroTask (/Users/mikecoxon/dev/ws-other/blog/node_modules/selenium-webdriver/lib/promise.js:2599:53)
From asynchronous test:
Error:
at Suite.<anonymous> (/Users/mikecoxon/dev/ws-other/blog/src/app/_services/post.service.spec.ts:20:3)
at addSpecsToSuite (/Users/mikecoxon/dev/ws-other/blog/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1107:25)
at Env.describe (/Users/mikecoxon/dev/ws-other/blog/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1074:7)
at describe (/Users/mikecoxon/dev/ws-other/blog/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4399:18)
at Object.<anonymous> (/Users/mikecoxon/dev/ws-other/blog/src/app/_services/post.service.spec.ts:16:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Module.m._compile (/Users/mikecoxon/dev/ws-other/blog/node_modules/ts-node/src/index.ts:473:23)
at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/mikecoxon/dev/ws-other/blog/node_modules/ts-node/src/index.ts:476:12)
经过测试的服务本身是一个相当标准的http服务,没有花哨的内容。我相当确定问题不存在,而是看起来像我的软件包依赖项(我认为)。我不确定如何最终获得一个非常新的打字稿,但是我不得不运行以下命令来重新编译所有内容:
$ npm i -g npm-check-updates
$ npm install -g npm
$ npm install
最后将打字稿从3.5.4降级为:
$ npm i typescript@3.4.5 --save-dev --save-exact
现在package.json
看起来像这样:
{
"name": "blog",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~8.1.1",
"@angular/common": "~8.1.1",
"@angular/compiler": "~8.1.1",
"@angular/core": "~8.1.1",
"@angular/forms": "~8.1.1",
"@angular/platform-browser": "~8.1.1",
"@angular/platform-browser-dynamic": "~8.1.1",
"@angular/router": "~8.1.1",
"ngx-markdown": "^8.1.0",
"ngx-markdown-editor": "^1.2.0",
"rxjs": "~6.5.2",
"tslib": "^1.10.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.801.1",
"@angular/cli": "~8.1.1",
"@angular/compiler-cli": "~8.1.1",
"@angular/language-service": "~8.1.1",
"@types/jasmine": "~3.3.13",
"@types/jasminewd2": "~2.0.6",
"@types/node": "~12.6.2",
"codelyzer": "^5.1.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.2.0",
"karma-chrome-launcher": "~3.0.0",
"karma-coverage-istanbul-reporter": "~2.0.5",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"protractor": "~5.4.2",
"ts-node": "~8.3.0",
"tslint": "~5.18.0",
"typescript": "3.5.3",
"zone.js": "^0.9.1"
}
}
我将经过测试的服务和app.component.ts放在参考app.component.ts,post.service.ts的代码笔中。
那么有人知道为什么会这样吗?实际上Failed: Can't resolve all parameters for ApplicationModule: (?).
到底是什么意思?
答案 0 :(得分:0)
假设您是http客户端,则还需要导入/注入Postservice依赖项:
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
以及与http api相关的其他依赖项
更新:
如前所述,您需要在上面进行导入以及在PostService中导入或模拟其余依赖项,例如:
import { environment } from '../../environments/environment.prod';
import { catchError, map, tap } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { Post } from '../_model/post';
import { ApiResponse } from '../_model/api.response';
import { ResponseHandler } from '../_helpers/response.handler';