[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 ...
随机推荐
- code blocks常用快捷键
CodeBlocks常用操作快捷键 编辑部分: Ctrl + A:全选 Ctrl + C:复制 Ctrl + X: 剪切 Ctrl + V:粘贴 Ctrl + Z:撤销(后退一步) Ctrl + S: ...
- 1.24 Python知识进阶 - 类与对象
类 语法格式: class Dog(object): print("the dog is barking ...") Dog为类名,object为要继承的基类,Dog类会从基类ob ...
- JavaFx lineChart real-time Monitor
JavaFx lineChart real-time Monitor about memory public class EffectTest extends Application { Stac ...
- 40.lombok在IntelliJ IDEA下的使用
转自:https://www.cnblogs.com/yjmyzz/p/lombok-with-intellij-idea.html lombok是一款可以精减java代码.提升开发人员生产效率的辅助 ...
- 94.文件bat脚本自删除
taskkill / f / im 自删除.exedel 自删除.exedel 1.bat void main() { FILE *pf = fopen("1.bat", &quo ...
- Android前后端交互细节--Json转化为对象的原理
移动互联网用户基数越来越大,除了一些工具类(指南针.手电筒等)的应用,绝大部分APP都需要与后端进行交互. 交互的数据格式有JSON.XML等,由于JSON具有语法简单.占用空间小等优势,基本所有的公 ...
- [python]bug和debug
bug:代码中存在的语法或者逻辑问题 debug:自查和解决代码中的问题 (coding五分钟,debug两小时) 一.出现bug原因的四大类型 1.粗心 1)错误案例 上面这个错误就是因为 if语句 ...
- [Ramda] Simple log function for debugging Compose function / Using R.tap for logging
const log = function(x){ console.log(x); return x; } const get = R.curry(function(prop, obj){ return ...
- 使用Tomcat发布war包
第一步:下载tomacat 1.下载地址:http://tomcat.apache.org 2.解压后目录如下 3.双击bin文件夹下startup.bat 即可启动tomcat, 计算机会弹出控制台 ...
- Excel Add-in
Excel Add-in 前言 这个系列文章应该有一阵子没有更新了,原因是一如既往的多,但是根本所在是我对于某些章节其实还没有完全想好怎么写,尤其是对于Office Add-in这块 —— 到底是要每 ...