[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 ...
随机推荐
- 求第k大的数(用到快速排序算法的思想)
//下面两种part效率比较:相同运算量下part比part2快5倍左右,part2写法简单但是效率低 #include "stdafx.h" #include <iostr ...
- css 兼容性前缀
一.不同浏览器内核下的书写规则 二:transform 具体变性中心基点 transform-origin 默认情况下 rotate旋转.scale缩放.translate位移.矩阵matri ...
- golang sync.Mutex(2)
package main import ( "fmt" "sync" "time" ) type User struct { Name st ...
- Spring MVC modelandview
一开始${Name} 不能显示 原来是: import org.springframework.web.portlet.ModelAndView; --错误的引入 import org.springf ...
- 【AIM Tech Round 4 (Div. 2) A】Diversity
[链接]http://codeforces.com/contest/844/problem/A [题意] 大水题 [题解] 看看不同的个数num是不是小于k,小于k,看看len-num够不够补的 [错 ...
- spring+mybatis+Atomikos JTA事务配置说明
一.概览 Atomikos是一个公司名字,旗下最著名的莫过于其Atomikos的事务管理器产品.产品分两个:一个是开源的TransactionEssentials,一个是商业的ExtremeTrans ...
- php訪问mysql数据库
PHP訪问Mysql数据库 PHP能够通过mysql接口和mysqli接口訪问mysql数据库. 须要加入mysql和mysqli接口才干訪问mysql数据库. windows下配置amp: a.安装 ...
- 如何从mysql数据库中取到随机的记录
如何从mysql数据库中取到随机的记录 一.总结 一句话总结:用随机函数newID(),select top N * from table_name order by newid() ----N是一个 ...
- 学习笔记(四):jQuery之动画效果
1.show()显示效果 语法:show(speed,callback) Number/String,Function speend为动画执行时间,单位为毫秒.也可以为slow"," ...
- JS /CSS 实现模态框(注册和登录组件)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...