In a proper unit test we want to isolate external dependencies as much as possible to guarantee a reliable test outcome. Http calls represent such external dependencies. Therefore, when testing our Angular components or services that rely on data retrieved via Http, we need to make sure to mock such calls and instead provide some fake data during the test execution. In this lesson we about the new HttpClientTestingModule and HttpTestingController that has been added in Angular v4.3.1 to make our life easier.

Serivce:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http'; export interface Person {
name: string;
} @Injectable()
export class PeopleService { constructor(private http: HttpClient) {} fetchPeople(): Observable<Person[]> {
return this.http
.get<Person[]>('/api/v1/people');
} }

Spec:

import { TestBed, inject } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { PeopleService } from './people.service'; describe('The PeopleService', () => { beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
PeopleService
]
});
}); it('should fetch a list of people', inject(
[PeopleService, HttpTestingController],
(peopleService: PeopleService, httpMock: HttpTestingController) => { // execute the call
peopleService
.fetchPeople()
.subscribe(people => {
expect(people.length).toBe(2);
expect(people[0].name).toBe('Juri');
}); const req = httpMock.expectOne('/api/v1/people', 'call to ppl api'); expect(req.request.method).toBe('GET'); req.flush([
{
name: 'xxx'
},
{
name: 'xxx'
}
]); httpMock.verify();
})); });

[Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests的更多相关文章

  1. [Unit Testing] Mock a Node module's dependencies using Proxyquire

    Sometimes when writing a unit test, you know that the module you're testing imports a module that yo ...

  2. [Unit Testing] Mock an HTTP request using Nock while unit testing

    When testing functions that make HTTP requests, it's not preferable for those requests to actually r ...

  3. C/C++ unit testing tools (39 found)---reference

    http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...

  4. [Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith

    Previously we have seen how to do Unit testing with Mockito; import org.junit.Test; import static or ...

  5. [Angular & Unit Testing] Testing a RouterOutlet component

    The way to test router componet needs a little bit setup, first we need to create a "router-stu ...

  6. [Angular & Unit Testing] Automatic change detection

    When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...

  7. [Angular & Unit Testing] Testing Component with Store

    When using Ngrx, we need to know how to test the component which has Router injected. Component: imp ...

  8. [Angular Unit Testing] Debug unit testing -- component rendering

    If sometime you want to log out the comonent html to see whether the html render correctly, you can ...

  9. [Angular Unit Testing] Testing Component methods

    import {ComponentFixture, TestBed} from '@angular/core/testing'; import {BrowserDynamicTestingModule ...

随机推荐

  1. [SharePoint][SharePoint Designer 入门经典]Chapter11 工作流基础

    1.SPS中可以创建的工作流的种类 2.SPD工作流基础 3.创建列表\库工作流 4.创建可重用的工作流 5.利用基于站点的工作流 6.SPD 工作流的限制和注意事项

  2. 大型Web 站点 Asp.net Session过期你怎么办

    在 WEB 系统中. 我们通常会用session来保存一些简单可是却非常重要的信息.比方Asp.net中常常会用Session来保存用户登录信息,比方UserID.为了解决 WEB场大家採用了把ses ...

  3. Ubuntu 16.04 安装 Wireshark分析tcpdump的pcap包——sudo apt install wireshark-qt

    tcpdump 的抓包保存到文件的命令参数是-w xxx.cap   抓eth1的包  tcpdump -i eth1 -w /tmp/xxx.cap    抓 192.168.1.123的包  tc ...

  4. Docker Compose + Spring Boot + Nginx + Mysql

    Docker Compose + Spring Boot + Nginx + Mysql 实践 我知道大家这段时间看了我写关于 docker 相关的几篇文章,不疼不痒的,仍然没有感受 docker 的 ...

  5. java.lang.ClassNotFoundException: org.objectweb.asm.ClassWriter

    转自:https://www.cnblogs.com/yfceshi/p/6814802.html Caused by: javax.xml.ws.WebServiceException: java. ...

  6. Python中functools模块函数解析

    Python自带的 functools 模块提供了一些常用的高阶函数,也就是用于处理其它函数的特殊函数.换言之,就是能使用该模块对可调用对象进行处理. functools模块函数概览 functool ...

  7. 常用相关linux命令

    查看进程netstat -tnlp | egrep "(9097)" lsof -i:9097 ps -ef | grep kafka 观察句柄变化lsof -p $pid | w ...

  8. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online J Press the Button

    BaoBao and DreamGrid are playing a game using a strange button. This button is attached to an LED li ...

  9. 为什么叫Unity3d为脚本语言

    初接触Unity,看到大家说的都是工作主要是写脚本语言. 一直纳闷为什么说脚本语言呢,c#可不是脚本语言啊. -- -- 后来释然,说它是脚本语言是因为传统程序都是由代码构成的(像iOS.Androi ...

  10. face++算法工程实习生面试

    2018-01-11 算法工程实习生  自动化工具链方面 面试的知识点非常仔细,十分检验基本功底 1.自我介绍 2.算法题,leetcode 第一题 两数之和 问python中数组和字典的查找时间复杂 ...