我有一个棱角分明的服务,进口第三方的依赖。我调用“依赖”给我浏览器指纹,然后将其存储在服务中。
我不知道如何在测试中模拟这个依赖项,因此我可以断言它已被调用并模拟返回值。
这是服务:
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);
});
});
});发布于 2018-10-18 09:36:21
我设法用下面的规范实现了这一点。我使用间谍和返回值来模拟指纹创建。
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);
});
});发布于 2018-11-01 12:18:14
我不会直接将第三方导入服务,因为很难对它们进行单元测试(特别是当它们执行一些棘手的事情,如http调用或DOM操作等时)。
创建像工厂一样适用于第三方的棱角服务也许是个好主意:
import * as Fingerprint2 from 'fingerprintjs2';
@Injectable()
export class FingerprintFactory {
create(): any {
return new Fingerprint2();
}
}之后,您可以将FingerprintFactory注入到ClientInfoService中,并使用其create方法创建Fingerprint2实例。
而且,在您的FingerprintFactory中模拟ClientInfoService也非常容易。
https://stackoverflow.com/questions/52836002
复制相似问题