angular-cn/aio/dist/generated/docs/guide/testing-services.json

5 lines
19 KiB
JSON
Raw Permalink Normal View History

{
"id": "guide/testing-services",
"title": "Testing services",
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/testing-services.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <h1 id=\"testing-services\">Testing services<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/testing-services#testing-services\"><i class=\"material-icons\">link</i></a></h1>\n<p>To check that your services are working as you intend, you can write tests specifically for them.</p>\n<div class=\"alert is-helpful\">\n<p> For the sample app that the testing guides describe, see the <live-example name=\"testing\" embedded-style=\"\" nodownload=\"\">sample app</live-example>.</p>\n<p> For the tests features in the testing guides, see <live-example name=\"testing\" stackblitz=\"specs\" nodownload=\"\">tests</live-example>.</p>\n</div>\n<p>Services are often the easiest files to unit test.\nHere are some synchronous and asynchronous unit tests of the <code>ValueService</code>\nwritten without assistance from Angular testing utilities.</p>\n<code-example path=\"testing/src/app/demo/demo.spec.ts\" region=\"ValueService\" header=\"app/demo/demo.spec.ts\">\n// Straight Jasmine testing without Angular's testing support\ndescribe('ValueService', () => {\n let service: ValueService;\n beforeEach(() => { service = new ValueService(); });\n\n it('#getValue should return real value', () => {\n expect(service.getValue()).toBe('real value');\n });\n\n it('#getObservableValue should return value from observable',\n (done: DoneFn) => {\n service.getObservableValue().subscribe(value => {\n expect(value).toBe('observable value');\n done();\n });\n });\n\n it('#getPromiseValue should return value from a promise',\n (done: DoneFn) => {\n service.getPromiseValue().then(value => {\n expect(value).toBe('promise value');\n done();\n });\n });\n});\n\n</code-example>\n<a id=\"services-with-dependencies\"></a>\n<h2 id=\"services-with-dependencies\">Services with dependencies<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/testing-services#services-with-dependencies\"><i class=\"material-icons\">link</i></a></h2>\n<p>Services often depend on other services that Angular injects into the constructor.\nIn many cases, it's easy to create and <em>inject</em> these dependencies by hand while\ncalling the service's constructor.</p>\n<p>The <code>MasterService</code> is a simple example:</p>\n<code-example path=\"testing/src/app/demo/demo.ts\" region=\"MasterService\" header=\"app/demo/demo.ts\">\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nexport class MasterService {\n constructor(private valueService: ValueService) { }\n getValue() { return this.valueService.getValue(); }\n}\n\n</code-example>\n<p><code>MasterService</code> delegates its only method, <code>getValue</code>, to the injected <code>ValueService</code>.</p>\n<p>Here are several ways to test it.</p>\n<code-example path=\"testing/src/app/demo/demo.spec.ts\" region=\"MasterService\" header=\"app/demo/demo.spec.ts\">\ndescribe('MasterService without Angular testing support', () => {\n let masterService: MasterService;\n\n it('#getValue should return real value from the real service', () => {\n masterService = new MasterService(new ValueService());\n expect(masterService.getValue()).toBe('real value');\n });\n\n it('#getValue should return faked value from a fakeService', () => {\n masterService = new MasterService(new FakeValueService());\n expect(masterService.getValue()).toBe('faked service value');\n });\n\n it('#getValue should return faked value from a fake object', () => {\n const fake = { getValue: () => 'fake value' };\n masterService = new MasterService(fake as ValueService);\n expect(masterService.getValue()).toBe('fake value');\n });\n\n
}