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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [Angular & Unit Testing] Automatic change detection

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

  4. [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 ...

  5. [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 ...

  6. [Angular Unit Testing] Testing Component methods

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

  7. [Angular Unit Testing] Shallow Pipe Testing

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

  8. [Angular + Unit] AngularJS Unit testing using Karma

    http://social.technet.microsoft.com/wiki/contents/articles/32300.angularjs-unit-testing-using-karma- ...

  9. [Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy

    // backend test beforeEach(inject(function (_$compile_, _$httpBackend_, _$rootScope_, _$state_, _Ann ...

随机推荐

  1. sqlserver 导入excel数据

    有的时候需要将excel数据导入到数据库中,这里介绍一下操作方法: 1.可能需要安装sqlserver的插件 [AccessDatabaseEngine],这个可以在网上早,很多. 2.安装插件后,右 ...

  2. MyEclipse 2016 安装/破解

    MyEclipse2016 C1 已经出现了!感觉好像不错的样子! 不多说了,开整... 好熟悉的界面,点击Next! 如上图标注1所示,请修改安装目录! 根据自己的喜好可以选择不同的版本,也可以安装 ...

  3. ps的入门

    ps的入门 http://www.cnblogs.com/qingci/archive/2012/09/20/2694728.html

  4. POJ 1874 畅通工程续(最短路模板题)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. 全新linux中通过编译方式安装nginx

    先去官网下载linux.tar.gz包 http://nginx.org/en/download.html   传到linxu中 解压tar包 在软件包nginx-1.15.9目录下对NGINX进行配 ...

  6. Fragment 实现的 分类 效果

    Fragment   实现的 分类 效果 布局文件的信息: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...

  7. 学习笔记:Vue——动态组件&异步组件

    动态组件 01.在动态组件上使用keep-alive,保持组件的状态,以避免反复重渲染导致的性能问题. <!-- 失活的组件将会被缓存!--> <keep-alive> < ...

  8. vsphere client 参数

    转自:http://blog.163.com/sword_111/blog/static/66589416201422964544918/ C:\Program Files (x86)\VMware\ ...

  9. jquery设置attr属性值

    1.返回属性值 $(selector).attr(attribute); 2.设置属性值 $(selector).attr(attribute,value); 3.设置多个属性值 $(selector ...

  10. CSS笔记 - fgm练习 2-7 - 简易选项卡

    练习地址 http://www.fgm.cc/learn/lesson2/07.html <style> body,ul,li{margin:0;padding:0;} body{font ...