{"action":"created","issue":{"url":"https://api.github.com/repos/jasmine/jasmine/issues/1459","repository_url":"https://api.github.com/repos/jasmine/jasmine","labels_url":"https://api.github.com/repos/jasmine/jasmine/issues/1459/labels{/name}","comments_url":"https://api.github.com/repos/jasmine/jasmine/issues/1459/comments","events_url":"https://api.github.com/repos/jasmine/jasmine/issues/1459/events","html_url":"https://github.com/jasmine/jasmine/issues/1459","id":279861635,"number":1459,"title":"Testing asnyc (Promise) methods in Angular 2 Services","user":{"login":"dasois","id":3832690,"avatar_url":"https://avatars0.githubusercontent.com/u/3832690?v=4","gravatar_id":"","url":"https://api.github.com/users/dasois","html_url":"https://github.com/dasois","followers_url":"https://api.github.com/users/dasois/followers","following_url":"https://api.github.com/users/dasois/following{/other_user}","gists_url":"https://api.github.com/users/dasois/gists{/gist_id}","starred_url":"https://api.github.com/users/dasois/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dasois/subscriptions","organizations_url":"https://api.github.com/users/dasois/orgs","repos_url":"https://api.github.com/users/dasois/repos","events_url":"https://api.github.com/users/dasois/events{/privacy}","received_events_url":"https://api.github.com/users/dasois/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-12-06T18:38:47Z","updated_at":"2017-12-11T07:26:05Z","closed_at":"2017-12-11T07:26:05Z","author_association":"NONE","body":"This is an interesting problem: I'm trying to test a Service, that uses the Ionic BarcodeScanner. I have a repo based on the [ionic unit-testing repository](https://github.com/ionic-team/ionic-unit-testing-example) in order to try testing. I'm mocking the BarcodeScanner.scan method via spyOn(..).and.callFake(..)\r\n\r\n## Expected Behavior\r\n<!--- If you're describing a bug, tell us what should happen -->\r\n<!--- If you're suggesting a change/improvement, tell us how it should work -->\r\nThe following test code should work for both components and services.\r\n\r\n## Current Behavior\r\n<!--- If describing a bug, tell us what happens instead of the expected behavior -->\r\n<!--- If suggesting a change/improvement, explain the difference from current behavior -->\r\nIt just works for components.\r\n\r\n## Possible Solution\r\n<!--- Not obligatory, but suggest a fix/reason for the bug, -->\r\n<!--- or ideas how to implement the addition or change -->\r\n\r\n## Suite that reproduces the behavior (for bugs)\r\n<!--- Provide a sample suite that reproduces the bug. -->\r\nThis works (component).\r\n```javascript\r\nit(\"should be able to set a spy on the scanner and test the component\", done => {\r\n        const testBC = \"123456\";\r\n        const spy = spyOn(TestBed.get(BarcodeScanner), \"scan\");\r\n        spy.and.callFake(() => {\r\n            return new Promise((resolve, reject) => {\r\n                resolve(testBC);\r\n            })\r\n        });\r\n\r\n            component.testScanner().then(res => {\r\n                expect(res).toBe(testBC);\r\n                done();\r\n            }, reason => {\r\n                expect(true).toBe(false);\r\n                done();\r\n            })\r\n    });\r\n```\r\nThis does not work (service):\r\n```javascript\r\nit(\"should be able to set a spy on the scanner and test the service\", done => {\r\n        const testBC = \"123456\";\r\n        const spy = spyOn(TestBed.get(BarcodeScanner), \"scan\");\r\n        spy.and.callFake(() => {\r\n            return new Promise((resolve, reject) => {\r\n                resolve(testBC);\r\n            })\r\n        });\r\n\r\n        inject([TestService], (service) => {\r\n            service.testScanner().then(res => {\r\n                expect(res).not.toBe(testBC);\r\n                done()\r\n            }, reason => {\r\n                expect(true).toBe(false);\r\n                done();\r\n            })\r\n        })\r\n    });\r\n```\r\n\r\n## Context\r\n<!--- How has this issue affected you? What are you trying to accomplish? -->\r\n<!--- Providing context helps us come up with a solution that is most useful in the real world -->\r\nI'm trying to unit test a service that uses the barcode-scanner and then does some stuff with the result..\r\n\r\n## Your Environment\r\n<!--- Include as many relevant details about the environment you experienced the bug in -->\r\n* Version used: 2.5.3\r\n* Environment name and version (e.g. Chrome 39, node.js 5.4): Chrome Version 62.0\r\n* Operating System and version (desktop or mobile): Arch Linux (desktop)\r\n* Link to your project: https://github.com/ionic-team/ionic-unit-testing-example"},"comment":{"url":"https://api.github.com/repos/jasmine/jasmine/issues/comments/350642251","html_url":"https://github.com/jasmine/jasmine/issues/1459#issuecomment-350642251","issue_url":"https://api.github.com/repos/jasmine/jasmine/issues/1459","id":350642251,"user":{"login":"dasois","id":3832690,"avatar_url":"https://avatars0.githubusercontent.com/u/3832690?v=4","gravatar_id":"","url":"https://api.github.com/users/dasois","html_url":"https://github.com/dasois","followers_url":"https://api.github.com/users/dasois/followers","following_url":"https://api.github.com/users/dasois/following{/other_user}","gists_url":"https://api.github.com/users/dasois/gists{/gist_id}","starred_url":"https://api.github.com/users/dasois/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dasois/subscriptions","organizations_url":"https://api.github.com/users/dasois/orgs","repos_url":"https://api.github.com/users/dasois/repos","events_url":"https://api.github.com/users/dasois/events{/privacy}","received_events_url":"https://api.github.com/users/dasois/received_events","type":"User","site_admin":false},"created_at":"2017-12-11T07:26:05Z","updated_at":"2017-12-11T07:26:05Z","author_association":"NONE","body":"I found the problem. I have forgotten to [call the inject function](https://stackoverflow.com/a/39604104/3085821).\r\n\r\nThank you for helping!"}}
{"actor":{"display_login":"dasois"}}