【问题标题】:angular 8 jest test `toHaveBeenCalledWith` fails, how to test api url calls?angular 8 jest test `toHaveBeenCalledWith` 失败,如何测试 api url 调用?
【发布时间】:2020-02-25 13:20:22
【问题描述】:

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

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 文件:

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();
    });

});

【问题讨论】:

    标签: angular jestjs angular8 angular-test


    【解决方案1】:

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

    准备

    describe('获取 susbsytem 集合', () => {

        it('should call get the subsytem collection', () => {
    
            const response = cold('(-a|)', { a: subsystems });
            http.get = jest.fn(() => response);
    

    执行

    因此您需要执行一些操作,然后检查结果。类似的东西

    service.get();
    

    断言

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

    说明

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

    getSubsystems() {
        // Some code lead
        // ...
        // One path leads to get
        if (some condition)
            return http.get('url');
    }
    

    为了测试场景

    it('should call the url when some condition is fulfilled')
    

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

    如果不做3,测试框架不知道要执行哪个单元(方法),所以什么也不执行。

    【讨论】:

    • 添加service.get(); ok 还是需要拨打真实服务?对于我的情况service.getSubsystem() - 你能确认一下吗?
    • 我添加了service.getSubSystems(); - 它工作正常。通过了。感谢您。你能解释一下这里到底发生了什么吗?
    • @Athansios kataras,-我同意。所以当我进行CRUD 测试时,如何检查createupdatedelete
    • 您的意思是从您的 api 中发布、放置和删除;由于这是单元测试,您只能检查是否已进行调用。它们应该与 CRUD 功能的实现隔离。如果您想进行端到端测试,那么您应该有一个检索 API 并编写所有逻辑来创建/更新/删除并断言您的检索 api 返回的内容。如果您进行了删除,则什么也没有,如果您进行了创建,则应返回信息,等等。
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多