导入第三方依赖性的Angular服务方法的单元测试

时间:2018-10-16 12:57:42

标签: angular unit-testing jestjs angular-services

我有一个Angular服务,可导入第三方依赖关系。我称依赖为我提供浏览器指纹,然后将其存储在服务中。

我不确定如何在测试中模拟此依赖关系,因此可以断言它已被调用并模拟返回值。

这是服务:

import { Inject, Injectable } from '@angular/core';
import * as Fingerprint2 from 'fingerprintjs2';

@Injectable()
export class ClientInfoService {
    public fingerprint: string | null = null;

    constructor() {
    }

    createFingerprint(): any {
        return new Fingerprint2();
    }

    setFingerprint(): void {
        let fprint = this.createFingerprint();
        setTimeout(() => fprint.get(hash => this.fingerprint = hash), 500);
    }

    getFingerprint(): string | null {
        return this.fingerprint;
    }

}

这是当前的测试代码:

import { TestBed } from '@angular/core/testing';
import { ClientInfoService } from './client-info.service';

describe('Client Info Service', () => {
    const hash = 'a6e5b498951af7c3033d0c7580ec5fc6';
    let service: ClientInfoService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [ClientInfoService],
        });
        service = TestBed.get(ClientInfoService);
    });

    test('should be defined', () => {
        expect(service).toBeDefined();
    });


    describe('get the fingerprint', () => {

        test('it should be null', () => {
            let fprint = service.getFingerprint();
            expect(fprint).toBeNull();
        });

        test('it should be the hash value', () => {
            service.fingerprint = hash;
            let fprint = service.getFingerprint();
            expect(fprint).toEqual(hash);
        });

    test('it should get the hash value after setting', () => {
        jest.useFakeTimers();
        service.createFingerprint = jest.fn().mockReturnValue(() => {
            return {
                get: function (cb) {
                    return cb(hash);
                }
            };
        });
        spyOn(service, 'createFingerprint');
        service.setFingerprint();
        jest.runAllTimers();
        expect(service.createFingerprint).toHaveBeenCalled();
        expect(service.fingerprint).toEqual(hash);
    });

    });

});

2 个答案:

答案 0 :(得分:1)

我不会将第3方直接导入服务,因为很难对其进行单元测试(尤其是如果他们做一些棘手的事情,例如http调用或DOM操作等)

创建对第三方来说像工厂一样工作的Angular服务可能是个好主意:

import * as Fingerprint2 from 'fingerprintjs2';

@Injectable()
export class FingerprintFactory {
    create(): any {
        return new Fingerprint2();
    }
}

之后,您可以将FingerprintFactory注入ClientInfoService中,并使用其create方法来创建Fingerprint2实例。

在您的FingerprintFactory中模拟ClientInfoService会很容易

答案 1 :(得分:0)

我设法通过以下规格实现了这一目标。我使用间谍并返回值来模拟指纹创建。

import { TestBed } from '@angular/core/testing';
import { ClientInfoService } from './client-info.service';

describe('Client Info Service', () => {
    const hash = 'a6e5b498951af7c3033d0c7580ec5fc6';
    let service: ClientInfoService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [ClientInfoService],
        });
        service = TestBed.get(ClientInfoService);
    });

    test('should be defined', () => {
        expect(service).toBeDefined();
    });

    test('it should set the fingerprint', () => {
        jest.useFakeTimers()
        let cb = (h) => {return h;};
        spyOn(service, 'createFingerprint').and.returnValue({
            get: (cb) => {
                return cb(hash);
            },
        });
        service.setFingerprint();
        jest.runAllTimers();
        expect(service.createFingerprint).toHaveBeenCalled();
        expect(service.fingerprint).toEqual(hash);
    });

    test('it should get the fingerprint', () => {
        let fprint = service.getFingerprint();
        expect(fprint).toEqual(service.fingerprint);
    });

});