Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 开玩笑地用测试覆盖抽象类方法_Javascript_Typescript_Jestjs - Fatal编程技术网

Javascript 开玩笑地用测试覆盖抽象类方法

Javascript 开玩笑地用测试覆盖抽象类方法,javascript,typescript,jestjs,Javascript,Typescript,Jestjs,我有一个抽象的通用服务类 export default abstract class GenericService<Type> implements CrudService<Type> { private readonly modifiedUrl: URL; public constructor(url: string) { this.modifiedUrl = new URL(url, window.location.href);

我有一个抽象的通用服务类

export default abstract class GenericService<Type> implements CrudService<Type> {
    private readonly modifiedUrl: URL;

    public constructor(url: string) {
        this.modifiedUrl = new URL(url, window.location.href);
    }

    public async get(path?: string, filter?: URLSearchParams): Promise<Type> {
        try {
            if (path) {
                this.modifiedUrl.href += `${path}`;
            }
            addQueryParams(this.modifiedUrl, filter);

            const response = await handleRequest(`${this.modifiedUrl}`, getFetchOptions('GET'));
            const data = await response.json();
            return (await data.data) ? data.data : data;
        } catch (error) {
            throw new Error(`Runtime error: ${error}`);
        }
    }
}

export async function handleRequest(input: RequestInfo, init: RequestInit): Promise<Response> {
    const response = await fetch(input, init);

    if (!response.ok) {
        throw new Error(`Network response was not ok: ${response}`);
    }

    return response;
}
这个测试是有效的,但是覆盖率统计显示它仍然没有被覆盖。
那么我该如何隐藏<代码> > <代码> >方法> > GeuleService < /C> >

< p>您可以考虑以下方法

GenericService.spec.js
从“/GenericService”导入GenericSerice;
类DummyService扩展了GenericSerice{}
描述(“GenericSerice”,()=>{
以前(()=>{
global.fetch=jest.fn();
});
描述(“由类扩展)”,()=>{
举个例子;
以前(()=>{
实例=新的DummyService();
});
描述(“方法获取”,()=>{
描述(“给定路径),()=>{
const mockPath=“/pa/th”;
描述(“接收成功响应”,()=>{
让结果;
const mockData={key:“mock value”};
之前(异步()=>{
global.fetch.mockClear();
global.fetch.mockResolvedValue({
好的,没错,
json:jest.fn().mockResolvedValue(mockData)
});
结果=等待实例.get(mockPath);
});
它(“应该返回数据”,()=>{
期望(结果).toEqual(模拟数据);
});
它(“应该请求正确的URL”,()=>{
expect(global.fetch).tohavencalledwith(
"http://localhost/undefined/pa/th",
{
方法:“获取”
}
);
});
});
});
});
});
});

检查

这回答了最初的问题,但我认为它的伸缩性不好。GenericService说它是抽象的,但实际上没有指定用于实现类的API。如果
genericservice
有十几个抽象属性和方法呢?现在,您的DummyService必须提供十几个存根实现。
jest.mock('../../components/crudTable/service/GenericService');
const genericService = GenericService;

export class DummyClass {
    public name: string = '';
}
export class DummyService extends GenericService<DummyClass> {}

describe('Generic Service', () => {
    it('1- spy prototype function', async () => {
        const spy = jest.spyOn(genericService.prototype, 'get');
        await genericService.prototype.get();
        expect(spy).toHaveBeenCalledTimes(1);
    });
    it('2- mock prototype function', async () => {
        const mockFn = jest.fn(genericService.prototype.get);
        await mockFn();
        expect(mockFn).toHaveBeenCalledTimes(1);
    });
    it('3- mock subclass function', async () => {
        const dummyService = new DummyService('test url');
        const mockFn = jest.fn(dummyService.get);
        await mockFn();
        expect(mockFn).toHaveBeenCalledTimes(1);
    });
});