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

Problem in Unit Test Environment #39

Closed
DanielSchuech opened this issue Feb 21, 2016 · 5 comments
Closed

Problem in Unit Test Environment #39

DanielSchuech opened this issue Feb 21, 2016 · 5 comments

Comments

@DanielSchuech
Copy link

I written an wrapper arround ng2-translate to perfectly fit my use cases. While testing i dont want to test only my wrapper that it just call your correct functions.

So my function calls translateService.use. The lang isn't available so getTranslation is called. There you load them and subscribe to the pending observable. In this subscription you are going to delete this.pending. This is my problem.

I ve a very similar testing setup like you. The only change is located in the answering of MockBackend conenctions. You are going to save them and answering them when you call your mockBackendResponse function. I will answer them directly when they appear.
So the Observable wil lbe resolvewd directly and the subscription will executed directly before the use function can subscribe to this.pending. This.pending is deleted and i got the following error:

TypeError: Cannot read property 'subscribe' of undefined
        at TranslateService.use (/.../node_modules/ng2-translate/src/translate.service.js:90:20)

The error seems to be correct thorugh my test environment.

My Question is why are you going to delete this.pending? I removed it and all your tests passes and I could find any problems while running ng2-translate. So is it possible to remove line 134 in translate.service.ts?

Greetings,
Daniel

@ocombe ocombe closed this as completed in f452c65 Feb 21, 2016
@ocombe
Copy link
Member

ocombe commented Feb 21, 2016

I now delete the pending observable in the "onComplete" handler, this should (hopefully) fix your problem, let me know if that's not the case.

@DanielSchuech
Copy link
Author

It doesn't solve my problem.

I am going to answer the request with the following code

backendResponses: string[] = [];
backend = injector.get(XHRBackend);
backend.connections.subscribe((connection: any) => {
        connection.mockRespond(
          new Response(new ResponseOptions({body: backendResponses[0]}))
        );
        backendResponses.splice(0, 1);
      });

So the request will be answered directly. this.pending will be deleted immediately and undefined will be returned in getTranslation.

I cant do it your way to save the connections and and answering them at the end of the test. I need to nest 2 requests.

Would there be a problem not to delete pending? I cant remember any problem.

@ocombe
Copy link
Member

ocombe commented Feb 23, 2016

Maybe not, I need to do more tests

@ocombe
Copy link
Member

ocombe commented Feb 28, 2016

If I don't set this.pending to undefined, since it is an observable based on http, it will request the translation file whenever a translation is needed :(
Let me find another way to test this.

ocombe added a commit that referenced this issue Feb 28, 2016
@ocombe
Copy link
Member

ocombe commented Feb 28, 2016

This has been fixed in 1.8.0 ! You shouldn't have a problem with your unit tests anymore :-)

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