{"action":"created","issue":{"url":"https://api.github.com/repos/angular/angular/issues/10972","repository_url":"https://api.github.com/repos/angular/angular","labels_url":"https://api.github.com/repos/angular/angular/issues/10972/labels{/name}","comments_url":"https://api.github.com/repos/angular/angular/issues/10972/comments","events_url":"https://api.github.com/repos/angular/angular/issues/10972/events","html_url":"https://github.com/angular/angular/issues/10972","id":172343774,"number":10972,"title":"unable to mock http response, when invoked from service constructor","user":{"login":"maciejsobala","id":15926528,"avatar_url":"https://avatars.githubusercontent.com/u/15926528?v=3","gravatar_id":"","url":"https://api.github.com/users/maciejsobala","html_url":"https://github.com/maciejsobala","followers_url":"https://api.github.com/users/maciejsobala/followers","following_url":"https://api.github.com/users/maciejsobala/following{/other_user}","gists_url":"https://api.github.com/users/maciejsobala/gists{/gist_id}","starred_url":"https://api.github.com/users/maciejsobala/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maciejsobala/subscriptions","organizations_url":"https://api.github.com/users/maciejsobala/orgs","repos_url":"https://api.github.com/users/maciejsobala/repos","events_url":"https://api.github.com/users/maciejsobala/events{/privacy}","received_events_url":"https://api.github.com/users/maciejsobala/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-08-21T21:56:54Z","updated_at":"2016-08-22T10:37:04Z","closed_at":null,"body":"\r\n```\r\n[ x ] bug report => search github for a similar issue or PR before submitting\r\n```\r\nGot following code:\r\n```\r\n@Injectable()\r\nexport class AttributeTypesService {\r\n\r\n  constructor(private http: Http) {\r\n    this.getItems();\r\n  }\r\n\r\n  public getItems() {\r\nconsole.log(\"get items\");\r\n    this.http.get(SOME_ENDPOINT).subscribe(value=>{\r\n      console.log(\"GOT HTTP RESPONSE\");\r\n      console.log(value);\r\n    });\r\n  }\r\n}\r\n```\r\n\r\nand following test:\r\n```\r\ndescribe(\"Products service\", () => {\r\n  beforeEach(() => {\r\n    TestBed.configureTestingModule({\r\n      providers: [\r\n\r\n        BaseRequestOptions,\r\n        MockBackend,\r\n        HttpModule,\r\n\r\n        provide(Http, {\r\n          useFactory: (mockBackend: ConnectionBackend,\r\n                       defaultOptions: BaseRequestOptions) => {\r\n            return new Http(mockBackend, defaultOptions);\r\n          }, deps: [MockBackend, BaseRequestOptions]}),\r\n\r\n        AttributeTypesService,\r\n      ],\r\n    });\r\n  });\r\n\r\n  describe(\"some suite\", () => {\r\n    it(\"some test\", inject([AttributeTypesService, MockBackend], fakeAsync((service, mockBackend) => {\r\n      mockBackend.connections.subscribe(c => {\r\n        console.log(\"BACKEND HIT\");\r\n        let response: ResponseOptions;\r\n        c.mockRespond(new Response(new ResponseOptions({status: 200, body: \"blablablabla\"})));\r\n      });\r\n\r\n      service.getItems();\r\n      console.log(\"test\");\r\n    })));\r\n  });\r\n});\r\n```\r\n**Current behavior**\r\nOutput from my test is:\r\n```\r\nLOG: 'get items'\r\nLOG: 'get items'\r\nLOG: 'BACKEND HIT'\r\nLOG: 'GOT HTTP RESPONSE'\r\nLOG: Response{_body: 'blablablabla', status: 200, ok: true, statusText: null, headers: null, type: null, url: null}\r\nLOG: 'test'\r\n```\r\n\r\n**Expected behavior**\r\nI am expecting following output:\r\n```\r\nLOG: 'get items'\r\nLOG: 'BACKEND HIT'\r\nLOG: 'GOT HTTP RESPONSE'\r\nLOG: Response{_body: 'blablablabla', status: 200, ok: true, statusText: null, headers: null, type: null, url: null}\r\nLOG: 'get items'\r\nLOG: 'BACKEND HIT'\r\nLOG: 'GOT HTTP RESPONSE'\r\nLOG: Response{_body: 'blablablabla', status: 200, ok: true, statusText: null, headers: null, type: null, url: null}\r\nLOG: 'test'\r\n```\r\n\r\nAs you can see `getItems()` method is invoked twice - one from test, and one from constructor - and it is logging `get items` twice. Unfornatelly it is touching mockBackend only once (when it is called directly from test).\r\n\r\n\r\n* **Angular version:** 2.0.0-rc.5\r\n \r\n* **Browser:** [all  ] \r\n \r\n* **Language:** [TypeScript 1.8 ] \r\n"},"comment":{"url":"https://api.github.com/repos/angular/angular/issues/comments/241375125","html_url":"https://github.com/angular/angular/issues/10972#issuecomment-241375125","issue_url":"https://api.github.com/repos/angular/angular/issues/10972","id":241375125,"user":{"login":"markpritchett","id":1267889,"avatar_url":"https://avatars.githubusercontent.com/u/1267889?v=3","gravatar_id":"","url":"https://api.github.com/users/markpritchett","html_url":"https://github.com/markpritchett","followers_url":"https://api.github.com/users/markpritchett/followers","following_url":"https://api.github.com/users/markpritchett/following{/other_user}","gists_url":"https://api.github.com/users/markpritchett/gists{/gist_id}","starred_url":"https://api.github.com/users/markpritchett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markpritchett/subscriptions","organizations_url":"https://api.github.com/users/markpritchett/orgs","repos_url":"https://api.github.com/users/markpritchett/repos","events_url":"https://api.github.com/users/markpritchett/events{/privacy}","received_events_url":"https://api.github.com/users/markpritchett/received_events","type":"User","site_admin":false},"created_at":"2016-08-22T10:37:04Z","updated_at":"2016-08-22T10:37:04Z","body":"Could you add a Plunker?  I previously copied a Plunker from @wardbell that demonstrated an issue with testing.  Here's mine if it helps - https://plnkr.co/edit/Ur4t5syhdXRVlEUP5zIl"}}
{"actor":{"display_login":"markpritchett"}}