Recommend to use angular-cli to generate component and service, so we can get testing templates.

  1. ng g s heros // generate a heros service

Component with injected service:

  1. import { TestBed, async, inject } from '@angular/core/testing';
  2. import { AppComponent } from './app.component';
  3. import {HerosService} from "./heros.service";
  4. import {By} from "@angular/platform-browser";
  5.  
  6. let fixture, comInstance, herosService, element, de;
  7.  
  8. describe('AppComponent', () => {
  9. beforeEach(() => {
  10. TestBed.configureTestingModule({
  11. declarations: [
  12. AppComponent
  13. ],
  14. providers: [
  15. HerosService
  16. ]
  17. });
  18. TestBed.compileComponents();
  19. });
  20.  
  21. beforeEach(() => {
  22. fixture = TestBed.createComponent(AppComponent);
  23. de = fixture.debugElement;
  24. comInstance = fixture.debugElement.componentInstance;
  25. herosService = fixture.debugElement.injector.get(HerosService);
  26. element = fixture.nativeElement; // to access DOM element
  27. });
  28.  
  29. it('should create the app', async(() => {
  30. expect(comInstance).toBeTruthy();
  31. }));
  32.  
  33. it(`should have as title 'app works!'`, async(() => {
  34. expect(comInstance.title).toEqual('app works!');
  35. }));
  36.  
  37. it('should render title in a h1 tag', async(() => {
  38. fixture.detectChanges();
  39. expect(element.querySelector('h1').textContent).toContain('app works!');
  40. }));
  41.  
  42. it('should able to change the title', async(() => {
  43. const expected = "Change title";
  44. comInstance.title = expected;
  45. fixture.detectChanges();
  46. fixture.whenStable().then(() => {
  47. expect(element.querySelector('h1').innerText).toBe(expected);
  48. expect(de.query(By.css('h1')).nativeElement.innerText).toBe(expected);
  49. });
  50. }));
  51.  
  52. it('should have HerosService defined', async(() => {
  53. const expected = herosService.foo();
  54. const result = "foo";
  55. expect(expected).toBe(result);
  56. }));
  57. });

Service:

  1. /* tslint:disable:no-unused-variable */
  2.  
  3. import { TestBed, async, inject } from '@angular/core/testing';
  4. import { HerosService } from './heros.service';
  5. import {HttpModule} from "@angular/http";
  6.  
  7. let service;
  8.  
  9. describe('HerosService', () => {
  10. beforeEach(() => {
  11. TestBed.configureTestingModule({
  12. imports: [ HttpModule ],
  13. providers: [
  14. HerosService
  15. ]
  16. });
  17. });
  18.  
  19. beforeEach(inject([HerosService], s => {
  20. service = s;
  21. }));
  22.  
  23. it('should ...', inject([HerosService], (service: HerosService) => {
  24. expect(service).toBeTruthy();
  25. }));
  26.  
  27. it('should able to get foo from foo()', inject([HerosService], service => {
  28. const expected = service.foo();
  29. const result = "foo";
  30. expect(expected).toBe(result);
  31. }));
  32.  
  33. it('should able to get heros from api', async(() => {
  34. service.getHeros()
  35. .subscribe(( heros )=> {
  36. expect(heros.length).toEqual();
  37. })
  38. }))
  39. });

[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. code blocks常用快捷键

    CodeBlocks常用操作快捷键 编辑部分: Ctrl + A:全选 Ctrl + C:复制 Ctrl + X: 剪切 Ctrl + V:粘贴 Ctrl + Z:撤销(后退一步) Ctrl + S: ...

  2. 1.24 Python知识进阶 - 类与对象

    类 语法格式: class Dog(object): print("the dog is barking ...") Dog为类名,object为要继承的基类,Dog类会从基类ob ...

  3. JavaFx lineChart real-time Monitor

    JavaFx lineChart real-time Monitor   about memory public class EffectTest extends Application { Stac ...

  4. 40.lombok在IntelliJ IDEA下的使用

    转自:https://www.cnblogs.com/yjmyzz/p/lombok-with-intellij-idea.html lombok是一款可以精减java代码.提升开发人员生产效率的辅助 ...

  5. 94.文件bat脚本自删除

    taskkill / f / im 自删除.exedel 自删除.exedel 1.bat void main() { FILE *pf = fopen("1.bat", &quo ...

  6. Android前后端交互细节--Json转化为对象的原理

    移动互联网用户基数越来越大,除了一些工具类(指南针.手电筒等)的应用,绝大部分APP都需要与后端进行交互. 交互的数据格式有JSON.XML等,由于JSON具有语法简单.占用空间小等优势,基本所有的公 ...

  7. [python]bug和debug

    bug:代码中存在的语法或者逻辑问题 debug:自查和解决代码中的问题 (coding五分钟,debug两小时) 一.出现bug原因的四大类型 1.粗心 1)错误案例 上面这个错误就是因为 if语句 ...

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

  9. 使用Tomcat发布war包

    第一步:下载tomacat 1.下载地址:http://tomcat.apache.org 2.解压后目录如下 3.双击bin文件夹下startup.bat 即可启动tomcat, 计算机会弹出控制台 ...

  10. Excel Add-in

    Excel Add-in 前言 这个系列文章应该有一阵子没有更新了,原因是一如既往的多,但是根本所在是我对于某些章节其实还没有完全想好怎么写,尤其是对于Office Add-in这块 —— 到底是要每 ...