[Angular Unit Testing] Testing Services with dependencies
import { Http, Response, ResponseOptions } from '@angular/http';
import { TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { StockInventoryService } from './stock-inventory.service';
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
function createResponse(body) {
return Observable.of(
new Response(new ResponseOptions({ body: JSON.stringify(body) }))
);
}
class MockHttp {
get() {
return createResponse([]);
}
}
const cartItems = [{ product_id: 1, quantity: 10 }, { product_id: 2, quantity: 5 }];
const productItems = [{ id: 1, price: 10, name: 'Test' }, { id: 2, price: 100, name: 'Another Test' }];
describe('StockInventoryService', () => {
let service: StockInventoryService;
let http: Http;
beforeEach(() => {
const bed = TestBed.configureTestingModule({
providers: [
StockInventoryService,
{ provide: Http, useClass: MockHttp }
]
});
http = bed.get(Http);
service = bed.get(StockInventoryService);
});
it('should get cart items', () => {
// [...cartItems]: do a copy
spyOn(http, 'get').and.returnValue(createResponse([...cartItems]));
service.getCartItems()
.subscribe((result) => {
expect(result.length).toBe(2);
expect(result).toEqual(cartItems);
});
});
it('should get product items', () => {
spyOn(http, 'get').and.returnValue(createResponse([...productItems]));
service.getProducts()
.subscribe((result) => {
expect(result.length).toBe(2);
expect(result).toEqual(productItems);
});
});
});
[Angular Unit Testing] Testing Services with dependencies的更多相关文章
- [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 re ...
- [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 ...
- [Angular Unit Testing] Shallow Pipe Testing
import { TestBed, ComponentFixture } from '@angular/core/testing'; import { BrowserDynamicTestingMod ...
- [Angular + Unit] AngularJS Unit testing using Karma
http://social.technet.microsoft.com/wiki/contents/articles/32300.angularjs-unit-testing-using-karma- ...
- [Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy
// backend test beforeEach(inject(function (_$compile_, _$httpBackend_, _$rootScope_, _$state_, _Ann ...
随机推荐
- IAR FOR STM8 学习笔记 固件库 GPIO
经过一番挣扎,还是决定使用官方的固件库了.. 从网上下一个STM8S的固件库,记得是FOR IAR的. 找到里面的IAR模板就可以开始用了. 这些都是直接写好的库函数,可以直接调用,但首先得先读懂,先 ...
- Flume的Agent
Flume的Agent text(“filename”):将文件filename作为数据源,按行发送 tail(“filename”):探测filename新产生的数据,按行发送出去 fsyslogT ...
- 可重入锁ReentrantLock--转载
突然被问到什么是可重入锁?脑袋里闪过了n中概念,最终没有找到,从网上学习一下. 原文地址:https://www.ibm.com/developerworks/cn/java/j-jtp10264/ ...
- golang标准包中文手册
golang标准包中文手册 http://files.cnblogs.com/files/rojas/liudiwu-pkgdoc-master.zip
- TypeScript基础知识
一. TypeScript是js的超集,可以应用所有js语法 二. 特点: 1. 优点 a. 可以在编译阶段就发现大部分错误,这总比在运行时候出错好 b. 同一目录下不同文件中,使用统一命名,会有命名 ...
- C语言的多行宏定义
一.多行宏定义的使用 最近在跟STM32L011K4T6低功耗的源代码,发现使用了多行的宏定义来封装函数,记得之前把\给删除掉,编译程序就一直报错. \是续行操作符,也就是宏定义一行写不完,需要多行写 ...
- amazeui学习笔记二(进阶开发1)--项目结构structure
amazeui学习笔记二(进阶开发1)--项目结构structure 一.总结 1.项目结构:是说的amazeui在github上面的项目结构,二次开发amazeui用 二.项目结构structure ...
- python报错Nonetype object is not iterable
https://www.cnblogs.com/zhaijiahui/p/8391701.html 参考链接:http://blog.csdn.net/dataspark/article/detail ...
- OC中对于属性的总结(@property)
在没有属性之前: 对成员变量进行改动都要用到设置器:setter来改动 Person *per =[[Person alloc] init]; 对象通过设置器对成员变量内容进行修该 [per setN ...
- 15.SpringBoot简介-SpringBoot是什么可以做什么
转自:https://blog.csdn.net/kingboyworld/article/details/77713743 在过去的两年时间里,最让人兴奋.回头率最高.最能改变游戏规则的东西,大概就 ...