Test for rejected promise with Jasmine

前端 未结 2 1225
孤城傲影
孤城傲影 2021-01-23 00:50

In my Angular2 app which uses AngularFire2, I have an AuthService which tries to authenticate anonymously with Firebase.

I am trying to write a test that ex

2条回答
  •  走了就别回头了
    2021-01-23 01:10

    I solved this problem by doing the following:

        describe('should reject promise', () => {
    
            let resolved: boolean;
            let rejected: boolean;
            let _e: any;
    
            beforeEach(function (done) {
                resolved = false;
                rejected = false;
                // ensure conditions here are such that myFn() should return a rejected promise
                service.myFn().then(() => {
                    resolved = true;
                    done();
                }).catch((e) => {
                    rejected = true;
                    _e = e;
                    done();
                });
            })
    
            it('should reject', () => {
                expect(resolved).toEqual(false);
                expect(rejected).toEqual(true);
                expect(_e.name).toEqual("MyCustomErrorName");
            });
        });
    

提交回复
热议问题