[Angular Testing] Unit Testing -- Test component and service.
Recommend to use angular-cli to generate component and service, so we can get testing templates.
ng g s heros // generate a heros service
Component with injected service:
import { TestBed, async, inject } from '@angular/core/testing';
import { AppComponent } from './app.component';
import {HerosService} from "./heros.service";
import {By} from "@angular/platform-browser";
let fixture, comInstance, herosService, element, de;
describe('AppComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
providers: [
HerosService
]
});
TestBed.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
de = fixture.debugElement;
comInstance = fixture.debugElement.componentInstance;
herosService = fixture.debugElement.injector.get(HerosService);
element = fixture.nativeElement; // to access DOM element
});
it('should create the app', async(() => {
expect(comInstance).toBeTruthy();
}));
it(`should have as title 'app works!'`, async(() => {
expect(comInstance.title).toEqual('app works!');
}));
it('should render title in a h1 tag', async(() => {
fixture.detectChanges();
expect(element.querySelector('h1').textContent).toContain('app works!');
}));
it('should able to change the title', async(() => {
const expected = "Change title";
comInstance.title = expected;
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(element.querySelector('h1').innerText).toBe(expected);
expect(de.query(By.css('h1')).nativeElement.innerText).toBe(expected);
});
}));
it('should have HerosService defined', async(() => {
const expected = herosService.foo();
const result = "foo";
expect(expected).toBe(result);
}));
});
Service:
/* tslint:disable:no-unused-variable */
import { TestBed, async, inject } from '@angular/core/testing';
import { HerosService } from './heros.service';
import {HttpModule} from "@angular/http";
let service;
describe('HerosService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpModule ],
providers: [
HerosService
]
});
});
beforeEach(inject([HerosService], s => {
service = s;
}));
it('should ...', inject([HerosService], (service: HerosService) => {
expect(service).toBeTruthy();
}));
it('should able to get foo from foo()', inject([HerosService], service => {
const expected = service.foo();
const result = "foo";
expect(expected).toBe(result);
}));
it('should able to get heros from api', async(() => {
service.getHeros()
.subscribe(( heros )=> {
expect(heros.length).toEqual();
})
}))
});
[Angular Testing] Unit Testing -- Test component and service.的更多相关文章
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- Unit Testing, Integration Testing and Functional Testing
转载自:https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional- ...
- [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] 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] AngularJS Unit testing using Karma
http://social.technet.microsoft.com/wiki/contents/articles/32300.angularjs-unit-testing-using-karma- ...
- [Unit Testing] AngularJS Unit Testing - Karma
Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...
- Unit Testing a zend-mvc application
Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...
- Unit Testing of Spring MVC Controllers: Configuration
Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
随机推荐
- win7旗舰版怎么降级到专业版
一.操作准备及注意事项 1.UltraISO光盘制作工具9.5 2.备份C盘及桌面文件 二.win7旗舰版改成专业版的步骤 1.当前系统为Win7 SP1 64位旗舰版: 2.按Win+R打开运行,输 ...
- Maven搭建hadoop环境报Missing artifact jdk.tools:jdk.tools:jar:1.7(5种办法,2种正解)
刚刚写的那一篇,是网上比较主流的解决办法. 鉴于实际情况,有伙伴的机器上没有遇到这个问题,我们再探究原因,最终还有4种情况需要说明. 先说,另外一种"正解". <depend ...
- pycharm快捷键、经常使用设置、配置管理
http://blog.csdn.net/pipisorry/article/details/39909057 本博客一直在同步更新中! 内容包括:pycharm学习技巧 Learning tips. ...
- php图像处理(thinkphp框架有相对强大的图像处理功能)
php图像处理(thinkphp框架有相对强大的图像处理功能) 一.总结 1.php处理图像:php处理图像需要安装外库(gd库) 2.gd库函数可以非常完美的操作图像:安装好库之后,这个库里面的函数 ...
- 1.3 Quick Start中 Step 1: Download the code官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 不要局限于,这个版本,我只是以最新的版本,来做个引子,让大家对官网的各个kafka版 ...
- [JSOI2009]计数问题
一个n*m的方格,初始时每个格子有一个整数权值.接下来每次有2种操作: 改变一个格子的权值: 求一个子矩阵中某种特定权值出现的个数. 输入输出格式 输入格式: 第一行有两个数N,M. 接下来N行,每行 ...
- serialport串口通讯
在.NET Framework 2.0中提供了SerialPort类,该类主要实现串口数据通信 = System.IO.Ports.SerialPort.GetPortNames();获取电脑有哪几个 ...
- HDOJ 5409 CRB and Graph 无向图缩块
无向图缩块后,以n所在的块为根节点,dp找每块中的最大值. 对于每一个桥的答案为两块中的较小的最大值和较小的最大值加1 CRB and Graph Time Limit: 8000/4000 MS ( ...
- JS实现穿墙效果(判断鼠标划入划出的方向)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- zookeeper分布式协调服务的使用一
Zookeeper是一个高性能,分布式的应用协调服务. 提供服务: 1.集群成员的管理(Group Membership) 2.分布式锁(Locking) 3.选主(Leader Election) ...