首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >角8 jest测试‘test’失败时,如何测试api url调用?

角8 jest测试‘test’失败时,如何测试api url调用?
EN

Stack Overflow用户
提问于 2019-10-30 09:19:32
回答 1查看 1.3K关注 0票数 2

在我的测试规范中,我想为两种东西投保。

代码语言:javascript
运行
复制
1. dummy data maching with model and get response.
2. find the `api` called with correct url.

我正在尝试这些,但出错的原因是:

SubsystemServiceService › get susbsytem collection › should call get the subsytem collection

此外,我想更新我的规格文件,以确保我的两个要求。有人能帮帮我吗?

这是我的spec.ts文件:

代码语言:javascript
运行
复制
import { TestBed } from '@angular/core/testing';
import { HttpClient } from '@angular/common/http';
import { cold } from 'jasmine-marbles';
import { ModelSubSystem } from './../models/model.subSystem';
import { SubsystemService } from './subsystem-service';

describe('SubsystemServiceService', () => {

    let service: SubsystemService;
    let http: HttpClient;

    const data1 = {
        Id: 0,
        Name: 'subsystem1',
        IsDeletePossible: true,
        CreatedBy: '',
        CreatedDate: new Date(),
        UpdatedBy: '',
        UpdatedDate: new Date(),
        UpdatedByName: '',
        CreatedByName: ''
    } as ModelSubSystem;

    const data2 = {
        Id: 0,
        Name: 'subsystem2',
        IsDeletePossible: true,
        CreatedBy: '',
        CreatedDate: new Date(),
        UpdatedBy: '',
        UpdatedDate: new Date(),
        UpdatedByName: '',
        CreatedByName: ''
    } as ModelSubSystem;

    const subsystems = [data1, data2];

    beforeEach(() => {

        TestBed.configureTestingModule({
            providers: [{ provide: HttpClient, useValue: { get: jest.fn() } }]
        });

        service = TestBed.get(SubsystemService);
        http = TestBed.get(HttpClient);

    });

    describe('get susbsytem collection', () => {

        it('should call get the subsytem collection', () => {

            const response = cold('(-a|)', { a: subsystems });
            http.get = jest.fn(() => response);

            expect(http.get).toHaveBeenCalledWith(`https://ewsanedevaoscmsapi01-as.websites.net/api/SubSystem`);

        });
    });

    it('should be created', () => {
        expect(service).toBeTruthy();
    });

});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-30 09:33:13

你的实际行动在哪里?在一个测试中,应该有三件事

准备工作

描述(“获取susbsytem集合”,() => {)

代码语言:javascript
运行
复制
    it('should call get the subsytem collection', () => {

        const response = cold('(-a|)', { a: subsystems });
        http.get = jest.fn(() => response);

执行

因此,您需要执行一些操作,然后检查结果。有点像

代码语言:javascript
运行
复制
service.get();

断言

代码语言:javascript
运行
复制
        expect(http.get).toHaveBeenCalledWith(`https://ewsanedevaoscmsapi01-as.websites.net/api/SubSystem`);

    });
});

解释

假设您想测试以下服务方法:

代码语言:javascript
运行
复制
getSubsystems() {
    // Some code lead
    // ...
    // One path leads to get
    if (some condition)
        return http.get('url');
}

为了测试这个场景

代码语言:javascript
运行
复制
it('should call the url when some condition is fulfilled')

您需要: 1.设置条件,以便实现“某些条件”。创建模拟响应和间谍const response = cold('(-a|)', { a: subsystems }); 3。创建一个服务实例并调用代码来测试getSubsystems() 4。断言在调用代码之后,已经用预期的参数调用了http.get。

如果不执行3,则测试框架不知道要执行哪个单元(方法),因此不执行任何操作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58622042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档