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. Linux下读写芯片的I2C寄存器

    要想在Linux下读写芯片的I2C寄存器,一般需要在Linux编写一份该芯片的I2C驱动,关于Linux下如何编写I2C驱动,前一篇文章<手把手教你写Linux I2C设备驱动>已经做了初 ...

  2. 团队作业-Beta冲刺(3)

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 这个作业要求在哪里 https://edu.cnblo ...

  3. [NPM] Update published npm packages using np

    When we want to update our package we need to do a few things: pull latest from our git remote, bump ...

  4. 用Promise对象封装JQuery的AJAX过程

    let jqPostAjaxPromise = function(param){ return new Promise(function(resolve, reject){ $.ajax({ url: ...

  5. 动态规划例子:Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  6. Java Web学习总结(18)——JSP标签

    一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...

  7. ARCGIS刷新的故事

    转自原文章ARCGIS部分刷新 1, IActiveView.Refresh 全局刷新,即重绘地图中的所有内容,是效率最低的一种刷新方法.当数据量大时非常耗时.所以除非绝对必要,一般推荐使用IActi ...

  8. c++中的相对路径

           今天在vs2010里读取相对路径下的图片文件出了点问题.于是查了一下相对路径的编程知识,记录下来分享给大家:      问题描写叙述:path=".\\TrainData\\& ...

  9. git stash备份当前工作区内容,回到最近一次commit提交

    git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致.同时,将当前的工作区内容保存到Git栈中.git stash pop: 从Git栈中读取 ...

  10. 芯片TPS70925

    TPS70925电源芯片 从上图中可以看出EN脚是使能脚,并且是高使能,低失能. tps70925的典型用法: 这个芯片有很多封装,我们用的是第一个: