0

How can I "detect changes" in testing Angular without testbed?

With a component, I will have this:

it('should work', async(() => {
  const fixture = TestBed.createComponent(TestComponent);
  // do some async call
  fixture.detectChanges();   // <=== This is what I want
  expect(something).toEqual(somethingElse);
});

With a service, I don't always have a testbed. How to tell angular to wait before asserting? Example:

it('should work', async(() => {
  const service = new MyService();
  const sub = service.emitter$.subscribe((val) => {
    // expect(val).toEqual(otherVal);
  });
  service.doAsyncWork();
});

How to get these connected? What if I have two phases to check (e.g. once before that async work, once after, but all after some initial period)?

1 Answer 1

1

use fakeAsync with the tick function to simulate time period.

it('should work', fakeAsync(() => {
  const fixture = TestBed.createComponent(TestComponent);
  // do some async call
  tick(10000);    
  expect(something).toEqual(somethingElse);
});
Sign up to request clarification or add additional context in comments.

1 Comment

That's a temporary solution, in some occasions I depend on things outside my control for this incoming data. Thanks though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.