[Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests
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的更多相关文章
- [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 ...
- [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 ...
- C/C++ unit testing tools (39 found)---reference
http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...
- [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 ...
- [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 ...
- [Angular & Unit Testing] Automatic change detection
When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...
- [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 ...
- [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 ...
- [Angular Unit Testing] Testing Component methods
import {ComponentFixture, TestBed} from '@angular/core/testing'; import {BrowserDynamicTestingModule ...
随机推荐
- EF Code First:实体映射,数据迁移,重构
经过EF的<第一篇>,我们已经把数据访问层基本搭建起来了,但并没有涉及实体关系.实体关系对于一个数据库系统来说至关重要,而且EF的各个实体之间的联系,实体之间的协作,联合查询等也都依赖于这 ...
- 60款开源云应用【Part 3】(60 Open Source Apps You Can Use in the Cloud)
60款开源云应用[Part 3](60 Open Source Apps You Can Use in the Cloud) 本篇翻译自http://www.datamation.com/open-s ...
- mysql设置远程訪问数据库的多种方法
问题:MySQL权限设置正确,但仍无法远程訪问.通过telnet发现3306port未打开. 分析:MySQL默认仅仅绑定127.0.0.1,即:仅仅有在本机才干訪问3306port. 解决:找到My ...
- 从C到C++(下)
继承 从一个类派生到另外一个类,使前者的所有特征在后者中自己主动可用. 他能够声明一些类型,这些类型能够共享部分或所有曾经所声明的类型.它也能够从超过一个的基类中共享一些特性. C++是支持多继承的. ...
- Spring整合TimerTask实现定时任务调度
一. 前言 近期在公司的项目中用到了定时任务, 本篇博文将会对TimerTask定时任务进行总结, 事实上TimerTask在实际项目中用的不多, 由于它不能在指定时间执行, 仅仅能让程序依照某一个频 ...
- 光标属性CSS cursor 属性
CSS cursor 属性 CSS cursor属性,以前不知道,如果以后用到自己看的 <html> <body> <p>请把鼠标移动到单词上,可以看到鼠标指针发生 ...
- 移动端H5页面编辑器开发实战--原理结构篇
很久前的写的文章了,转载下发到这里 原文地址: https://blog.csdn.net/tech_meizu/article/details/52288797
- 参照实验室这边案例做一个简单的maven webapp项目
流程 : 首先写出一个简单的前端页面. 之后写配置文件.dao.domain等等,注意这里可以使用generator进行自动配置 实验室这边配置文件如下: 其实主要的配置文件就分为6“个”. appl ...
- ros中文术语表及消息类型表
前言:整理一些ros常用表格,包括中文术语对照表. 一.中文术语表 二.消息类型表 -END-
- Activity、Fragment、ViewPage
1.新建super //super提供统一的FragmentActivity入口.public abstract class SuperFragmentActivity extends Fragmen ...