Reputation: 177
I'm injecting a service like this:
it('name of test', inject([ Service], (hcs: Service) => {
const pipe = new MyPipe(hcs);
const expectedResult = ...
//Here the constructor of the hcs-service has to be completet, otherwise the Pipe fails
const result = pipe.transform(...);
expect(result).toEqual(expectedResult);
}));
I need to run the constructor of the service before I start executing the transform method of my pipe. During runtime, this is not an issue as this pipe is always a reaction to a user action. But in my tests, it fails because the constructor hasn't run yet.
What would be a good way to solve this?
Edit: As Arif pointed out in a comment. The problem is that my constructor executes async tasks. Thanks :)
Upvotes: 1
Views: 1307
Reputation: 1643
Object's constructor will always be executed first. Unless you have an async code there.
As you mentioned in comments that was the problem in your case.
To wait in your test for async function to complete you have to use fakeAsync and tick
it('name of test', fakeAsync(inject([ Service], (hcs: Service) => {
const pipe = new MyPipe(hcs);
tick();
const expectedResult = ...
//Here the constructor of the hcs-service has to be completet, otherwise the Pipe fails
const result = pipe.transform(...);
expect(result).toEqual(expectedResult);
})));
Upvotes: 1