[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 ...
随机推荐
- Java循环链表实现约瑟夫环(搬运)
public class Josephus { static class Node{ int val; Node next; Node(int v){ val=v; } }//成员类,代表节点,类似于 ...
- Linux系统捕获数据包流程
Linux系统捕获数据包流程 为了提高数据包的捕获效率,瓶颈问题是一个需要非常关注的焦点.减少在捕获数据包过程中的瓶颈,就能够提高数据包捕获的整体性能.下面本文将以Linux操作系统为平台,分析捕获数 ...
- POJ 1082 Calendar Game
Adam and Eve enter this year's ACM International Collegiate Programming Contest. Last night, they pl ...
- BZOJ2631: tree(LCT)
Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2 ...
- linux下多进程的文件拷贝与进程相关的一些基础知识
之前实现了用文件IO的方式能够实现文件的拷贝,那么对于进程而言,我们是否也能够实现呢? 答案是肯定的. 进程资源: 首先我们先回想一下,进程的执行须要哪些资源呢?其资源包含CPU资源,内存资源,当然还 ...
- ubuntu-系统密匙
1.安装虚拟机xp,需要密匙,网上搜了一个,验证能用 MRX3F-47B9T-2487J-KWKMF-RPWBY 2.安装vm也需要密匙,如下可用 HC6JC-FPJ4M-RZM61-48852-2A ...
- 108.sqllite3(C语言数据库库)详解
//创建数据库,插入表,生效 //创建数据库,插入表,生效 void create_database() { //数据库指针 sqlite3 *db=; //打开数据数据库,初始化指针 int res ...
- JavaScript---call()使用的一些疑问
疑问:在使用.call()时,调用对象到底是否可以直接拥有了被调用者的方法和属性? 这里输出结果为:ReferenceError: o is not defined function Person(n ...
- MSDN上的异步socket 服务端例子
MSDN上的异步socket 服务端例子 2006-11-22 17:12:01| 分类: 代码学习 | 标签: |字号大中小 订阅 Imports SystemImports Syste ...
- android之路Gallery 画廊
Gallery是一个内部元素能够水平滚动,而且能够把当前选择的子元素定位在它中心的布局组件. 我们还是直接看看样例的执行效果. watermark/2/text/aHR0cDovL2Jsb2cuY3N ...