I am trying to get results from a dummy backend in Angular 2 for unit testing. We are currently using fakeAsynca timeout to simulate time.
fakeAsync
current working unit test
it('timeout (fakeAsync/tick)', fakeAsync(() => { counter.getTimeout(); tick(3000); //manually specify the waiting time }));
But this means that we are limited by a manually defined timeout. Not after completing an asynchronous task. What I'm trying to do is make it tick()wait for the task to complete before continuing with testing.
tick()
This does not seem to work as intended.
Reading on fakeAsyncand the tickanswer here explains that:
tick
tick () imitates the asynchronous course of time.
I created a plnkr example simulating this scenario.
getTimeout() -. tick() getTimeout().
getTimeout()
counter.ts
getTimeout() { setTimeout(() => { console.log('timeout') },3000) }
counter.specs.ts
it('timeout (fakeAsync/tick)', fakeAsync(() => { counter.getTimeout(); tick(); }));
": 1 ".
- ?
tick() , -? , ?
:
fixture.destroy(); flush();
// I had to do this: it('timeout (fakeAsync/tick)', (done) => { fixture.whenStable().then(() => { counter.getTimeout(); tick(); done(); }); });
flushMicrotasks . , tick() flushMicrotasks, jasmine tick().
fakeAsync - . tick - , , . , async whenStable, , 3 , .
async
whenStable
, counter.spec.ts , , 0 ( ). , , , . , , - .
, , fakeAsync tick , . - , , - setTimeout setTimeout , , , .
setTimeout
, , , , , . :
tick(Infinity);
, . ,
discardPeriodicTasks();
.
test.service.ts
export class TestService { getTimeout() { setTimeout(() => { console.log("test") }, 3000); } }
test.service.spec.ts
import { TestBed, async } from '@angular/core/testing'; describe("TestService", () => { let service: TestService; beforeEach(() => { TestBed.configureTestingModule({ providers: [TestService], }); service = TestBed.get(TestService); }); it("timeout test", async(() => { service.getTimeout(); }); });
export class TestService { readonly WAIT_TIME = 3000; getTimeout() { setTimeout(() => { console.log("test") }, this.WAIT_TIME); } }
import { TestBed, fakeAsync } from '@angular/core/testing'; describe("TestService", () => { let service: TestService; beforeEach(() => { TestBed.configureTestingModule({ providers: [TestService], }); service = TestBed.get(TestService); }); it("timeout test", fakeAsync(() => { service.getTimeout(); tick(service.WAIT_TIME + 10); }); });
Source: https://habr.com/ru/post/1673373/More articles:Custom Wpf window, Windows window resizing function - c #golang (* interface {}) (nil) равен нулю или нет? - goGet MAX and MIN in SQL string - sqlAdd new colors along with custom theme colors angular2 - angularHow to get all device tokens from Firebase? - react-nativeCustom URI schemes are not allowed for type "Web" - Google with Firebase - ios"Installing yarn" causes "network connectivity problems" - ruby-on-rails-4An advanced PWA web application, caching index.php is possible - cachingИзвлечь объект из изображения коробки с объектом - pythonPlotting multiple pandas scatter plots - pythonAll Articles