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

  1. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  2. Unit Testing, Integration Testing and Functional Testing

    转载自:https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional- ...

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

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

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

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

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

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

  8. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  9. Unit Testing of Spring MVC Controllers: Configuration

    Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

随机推荐

  1. js 限制只能输入数字小数点

    function checkNum(e) { var re = /^\d+(?=\.{0,1}\d+$|$)/ if (e.value != "") { if (!re.test( ...

  2. 1.25 Python知识进阶 - 封装

    封装 示例代码: class Role(object): count = 0 def __init__(self,name,role,weapon,life_value=100,money=15000 ...

  3. CListCtrl 隔行变色

    响应消息 ON_NOTIFY(NM_CUSTOMDRAW, ListCtrl的ID, OnNMCustomdrawList) 实现函数OnNMCustomdrawList void CFinishWe ...

  4. ListCtrl添加右键菜单(ListCtrl类里编辑,给ListCtrl 发送NM_RCLICK消息)

    在开发中会用到右键菜单,我们来一起学习一下. 假如,我们现在已经准备好了列表,就差右键处理了. 1.在资源视图中的添加一个MENU,如图 2.给要添加右键菜单的ListCtrl子类,添加消息 按 ct ...

  5. STM32W108无线射频模块串行通信接口编程实例

    STM32W108无线射频模块UART通信应用实例 基于STM32W108芯片,编写串口測试程序,測试串口通信.完毕PC通过串口与STM32W108进行通信. 开发环境与硬件平台 硬件:STM32W1 ...

  6. python内存增长问题

    如果你的程序没有调用什么特殊的库, 只是用了很平常的库, 而且使再循环很多的情况下, 那么建议你把循环里的程序拆出来,写成一子函数,循环子函数. 如下面格式: for   (循环) 子函数 这样程序每 ...

  7. 51Nod——N1082 与7无关的数

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1082 题目来源: 有道难题 基准时间限制:1 秒 空间限制:13107 ...

  8. 主定理(Master Theorem)与时间复杂度

    1. 问题 Karatsuba 大整数的快速乘积算法的运行时间(时间复杂度的递推关系式)为 T(n)=O(n)+4⋅T(n/2),求其最终的时间复杂度. 2. 主定理的内容 3. 分析 所以根据主定理 ...

  9. 一个开源.net混淆器——ConfuserEx (收藏)

    一个开源.net混淆器——ConfuserEx http://yck1509.github.io/ConfuserEx/ 由于项目中要用到.net 混淆器,网上搜寻了很多款,比如Dotfuscator ...

  10. 关于javascript中私有作用域的预解释

    1.如何区分私有变量还是全局变量 1).在全局作用域下声明(预解释的时候)的变量是全局变量 2).在“私有作用域中声明的变量”和“函数的形参”都是私有变量 在私有作用域中,我们代码执行的时候遇到一个变 ...