Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http service #169

Closed
hfwittmann opened this issue Nov 9, 2016 · 7 comments
Closed

Http service #169

hfwittmann opened this issue Nov 9, 2016 · 7 comments

Comments

@hfwittmann
Copy link
Contributor

Excellent work.

Everything works for me in the current state. So this is not really an issue. I am not sure where to post it.

I've tried to get his to work with a service that itself uses http. Everything I've tried has failed. One problem seems to be that there have been quite a few changes involving http, so anything that is more than a couple of months old does not work. On the other hand it's a very common use case. I'd be interested in a solution using either a real or mocked up service.

I would appreciate any hints, pointers or links.

cheers, Felix

@lathonez
Copy link
Owner

lathonez commented Nov 9, 2016

Thanks Felix.

For unit testing, I don't think there's conceptually any difference between http and any other service you may have to mock out?

Here's how we do it with Ionic's storage service:

  • create a mock file for the http framework you are using (e.g. whatever you call get or request on)
  • in your http service, create a static initHttp function to return an instance of the http framework on init (makes it much easier to mock it during testing)
  • in the beforeEach function of your tests, spyOn your initHttp function and return an instance of your mock

I have a closed source project unit testing a http service much the same way.

For E2E, see angular/protractor#3092

@hfwittmann
Copy link
Contributor Author

Hi Stephen,

thanks for your answer.

You're right, this works, I was maybe being unclear.

Taking the example storage.mock.ts, I would like the content from lines 7 -25 put in a separate json-file called mycontents.json. Then in storage.mock.ts one could have something like this: http.get('mycontents.json').subscribe(..)

The reason why I would like to have this is code/content separation. This would be especially interesting if the "content" is rather longer and more unwieldy than in storage.mock.ts,

But maybe not the way to go.

Cheers,Felix

@hfwittmann
Copy link
Contributor Author

Hi Stephen,

I am just now trying to follow your recommendation.

However I am hitting on the problem when I try to create a static initHttp.

Using

public static initHttp(): Http { return new Http(); }
I get this error message:

[ts] Supplied parameters do not match any signature of call target.
(alias) new Http(_backend: ConnectionBackend, _defaultOptions: RequestOptions): Http
import Http

Any recommendations?

Cheers,Felix

@lathonez
Copy link
Owner

The reason why I would like to have this is code/content separation. This would be especially interesting if the "content" is rather longer and more unwieldy than in storage.mock.ts

I think this is a good idea, but why do you need to import the content as JSON over http?

http-contents.mock.ts:

export const myContents = {
    "some": "json"
}

http.mock.ts:

import { myContents } from './http-contents.mock'

@lathonez
Copy link
Owner

[ts] Supplied parameters do not match any signature of call target. (alias) new Http(_backend: ConnectionBackend, _defaultOptions: RequestOptions): Http import Http

You seem to be using angular2/http, which is injected into the constructor via angular's DI. This is a much simpler case, no need for a static initHTTP method.

There's good examples of how to do this in ClickerService and the associated spec

http.ts:

constructor(public http: Http) {}

http.spec.ts:

let http: HttpService = null;

describe('HttpService', () => {

  beforeEach(() => {
    http = new HttpService(<any>new HttpMock());
  });

@hfwittmann
Copy link
Contributor Author

Excellent, thank you very much.

@lathonez
Copy link
Owner

#191

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants