Hi everyone!

What is the best way to test async method?

Async method authAPI.signIn() return a promise

return this.http.post('urlString', requestBodyObject).toPromise();

*Method 1: write normal code in method and use setTimeout hack in test (for 
authAPI use mock class, that return promise without using angular2 http 
service)*

*Example for method*

public signIn(form: any): void {
        var self = this;

        self.notify.clear('signin');

        if (form._status === 'VALID') {
            self.loading = true;
            var sendData = {
                email: form._value.email,
                password: form._value.password
            };
            self.authAPI.signIn(sendData)
                .then(() => {
                    return self.authAPI.getUser();
                })
                .then(() => {
                    self.loading = false;
                    self.navCtrl.setRoot(<Type>DashboardsPage);
                })
                .catch((error: {
                    status: number;
                    message: string;
                }) => {
                    self.loading = false;
                    self.notify.fail('signin', error.message);
                });
        } else {
            self.formError.show(form.controls, 'signin');
        }
    }

*Example for test:*

it('should sign in if all data correct', (done) => {
        spyOn(navCtrl, 'setRoot');

        signInPage.signIn({_status: 'VALID', _value: {email: 'xxx', 
password: 'yyy'}});

        setTimeout(() => {
            expect(navCtrl.setRoot).toHaveBeenCalled();
            done();
        }, 0);
    });

*Method 2: write method that always return a promise and use promise in 
test (for authAPI use mock class, that return promise without using 
angular2 http service)*

*Example method:*

public signIn(form: any): Promise<any> {
        var self = this;

        self.notify.clear('signin');

        if (form._status === 'VALID') {
            self.loading = true;
            var sendData = {
                email: form._value.email,
                password: form._value.password
            };
            return self.authAPI.signIn(sendData)
                .then(() => {
                    return self.authAPI.getUser();
                })
                .then(() => {
                    self.loading = false;
                    self.navCtrl.setRoot(<Type>DashboardsPage);
                })
                .catch((error: {
                    status: number;
                    message: string;
                }) => {
                    self.loading = false;
                    self.notify.fail('signin', error.message);
                });
        } else {
            self.formError.show(form.controls, 'signin');
            return Promise.resolve();
        }
    }

*Example test:*

it('should sign in if all data correct', (done) => {
        spyOn(navCtrl, 'setRoot');

        signInPage.signIn({_status: 'VALID', _value: {email: 'xxx', 
password: 'yyy'}})
            .then(() => {
                expect(navCtrl.setRoot).toHaveBeenCalled();
                done();
            });
    });

*Method 3: Somehow not use authAPI mock and work with MockBackend 
<https://angular.io/docs/js/latest/api/http/testing/MockBackend-class.html>*

In angular1 I used combination of $rootScope.$digest() + 
$httpBackend.flush(), but I can't find analog for angular2.

In addition, when I trying to provide real authAPI, I need to include all 
dependencies for it and all dependencies for it dependencies and, 
recursively, repeat this operation again until include all dependencies.

Can anybody help me?

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to