Help us understand the problem. What is going on with this article?

Angular test memo

  1. spy should be set before TestBed.get
  2. service should be instantiated in 'it' clause
  3. TestBed can be used in beforeEach.

Example

describe('AuthService', () => {

    /* set spy of angularFireAuth */
    let angularFireAuthStub: AngularFireAuth;
    let userStub;
    /* stub of firebase user */
    const tokenString = 'token_string';

    let service: AuthService;
    let userStateStub;
    beforeEach(() => {
        userStub = {
            getIdToken(): Promise<string | null> {
                return Promise.resolve(tokenString);
            },
            get providerData() {
                return [ {
                    providerId: 'google.com'
                }, {
                    providerId: 'password',
                } ];
            },
            get emailVerified() {
                return false;
            }
        };
        userStateStub = new BehaviorSubject(userStub);
        angularFireAuthStub = {
            get authState(): Observable<any> {
                return userStateStub;
            },
            get idToken(): Observable<string | null> {
                return this.authState.pipe(flatMap((user: any) => {
                    if (user) {
                        return user.getIdToken();
                    } else {
                        return of(null);
                    }
                }));
            },
            auth: {
                signOut(): Promise<void> {
                    return Promise.resolve();
                }
            } as FirebaseAuth
        } as AngularFireAuth;
        TestBed.configureTestingModule({
            providers: [
                AuthService,
                {provide: AngularFireAuth, useValue: angularFireAuthStub}
            ]
        });
    });


    it('#authState should return observable of user', function (done) {
        service = TestBed.get(AuthService);
        const result = service.authState;
        expect(result).toBeInstanceOf(Observable);
        result.subscribe(user => {
            expect(user).toEqual(userStub as any);
            done();
        });
    });

    it('#getAuthState should close after called', function (done) {
        service = TestBed.get(AuthService);
        const result = service.getAuthState;
        expect(result).toBeInstanceOf(Observable);
        const subscription = result.subscribe();
        expect(subscription.closed).toBeTrue();
        done();
    });

    it('#getIdToken should return observable of idToken', function (done) {
        service = TestBed.get(AuthService);
        expect(service.getIdToken).toBeInstanceOf(Observable);
        service.getIdToken.subscribe(token => {
            expect(token).toEqual(tokenString);
            done();
        });
    });

    it('#getIdToken should return null when authState return null', function (done) {
        spyOnProperty(angularFireAuthStub, 'authState').and.returnValue(new BehaviorSubject(null));
        service = TestBed.get(AuthService);
        expect(service.getIdToken).toBeInstanceOf(Observable);
        service.getIdToken.subscribe(token => {
            expect(token).toBeNull();
            done();
        });
    });

    it('#isSignIn should return true', function (done) {
        service = TestBed.get(AuthService);
        service.isSignIn().subscribe(result => {
            expect(result).toBeTrue();
            done();
        });
    });

    it('#isSignIn should return false if angularFireAuth.authState return null', function (done) {
        spyOnProperty(angularFireAuthStub, 'authState').and.returnValue(new BehaviorSubject(null));
        service = TestBed.get(AuthService);
        service.isSignIn().subscribe(result => {
            expect(result).toBeFalse();
            done();
        });
    });

    it('# isSignIn fail if email not verified', function (done) {
        spyOnProperty(userStub, 'providerData').and.returnValue([ {
            providerId: 'password',
        } ]);
        spyOnProperty(userStub, 'emailVerified').and.returnValue(false);
        service = TestBed.get(AuthService);
        service.isSignIn().subscribe(result => {
            expect(result).toBeFalse();
            done();
        });
    });

    it('#signOut should call firebaseAuth.auth.signOut', async function () {
        spyOn(angularFireAuthStub.auth, 'signOut');
        service = TestBed.get(AuthService);
        await service.signOut();
        expect(angularFireAuthStub.auth.signOut).toHaveBeenCalled();
    });
});

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした