:marked We write **unit tests** to explore and confirm the **behavior** of parts of our application. 我们编写**单元测试**来探索并巩固应用程序中的**行为**部分。 1. They **guard** against breaking existing code (“regressions”) when we make changes. 1. 当我们做出修改时,它们会**守护**已有代码,防止其被破坏。 1. They **clarify** what the code does both when used as intended and when faced with deviant conditions. 1. 当我们正确使用代码和面对异常情况时,它们会让代码的用途更加**明晰**。 1. They **reveal** mistakes in design and implementation. Tests force us to look at our code from many angles. When a part of our application seems hard to test, we may have discovered a design flaw, something we can cure now rather than later when it becomes expensive to fix. 1. 它们会**揭露**我们设计和实现中的错误。测试会强迫我们从多种角度看代码。如果应用程序的一个部分看起来很难测试,这可能就意味着存在设计缺陷。 我们可以立即修复它,而不用等到它变得不可收拾的那一天。 .alert.is-important :marked These testing chapters were written before the Angular 2 Beta release and are scheduled for significant updates. Much of the material remains accurate and relevant but references to specific features of Angular 2 and the Angular 2 testing library may not be correct. Please bear with us. 这些关于测试的章节写于Angular 2 Beta版发布之前,并且已经准备做重大更新。 这些素材中很多都是准确而有意义的,但是所引用的Angular 2特有特性和测试库可能是不正确的。各位见谅。 :marked Here is what we’ll learn in the unit testing chapters. 下面是我们将在单元测试一章中学到的: 1. Jasmine Testing 101 1. Jasmine测试简介 - setup to run Jasmine tests in the browser - 在浏览器中设置并准备运行Jasmine测试 - basic Jasmine testing skills - 基本的Jasmine测试技能 - write simple Jasmine tests in TypeScript - 用TypeScript写一个简单的Jasmine测试 - debug a test in the browser - 在浏览器中调试一个测试 1. The Application Under Test 1. 待测试的应用程序 1. Test a class 1. 测试一个类 - test a simple application class outside of Angular - 在Angular测试环境外测试简单的应用类 - where to put the test file - 把测试文件放在哪里 - load a test file with systemJS - 用SystemJS加载测试文件 1. Test a Pipe 1. 测试一个管道 - test a simple Angular Pipe class - 测试一个简单的Angular管道类 - add the Angular 2 library to the test harness - 把Angualr 2库添加到测试挽具(harness)中 - load multiple test files using system.js - 使用SystemJS加载多个测试文件 1. Test an Asynchronous Service 1. 测试异步服务 - test an asynchronous service class outside of Angular - 在Angular测试环境外测试异步服务 - write a test plan in code - 用代码写一个测试计划 - fake a dependency - 伪造一个依赖 - master the `catch(fail).then(done)` pattern - 掌握`catch(fail).then(done)`模式 - move setup to `beforeEach` - 把设置代码移入`beforeEach` - test when a dependency fails - 测试依赖失败时的情况 - control async test timeout - 控制异步测试的超时时间 1. The Angular Test Environment 1. Angular测试环境 - the Angular test environment and why we need help - Angular测试环境以及我们为什么需要它的帮助 - add the Angular Test libraries to the test harness - 把Angular测试库加入测试挽具中 - test the same async service using Angular Dependency Injection - 用Angular依赖注入测试同一个异步服务 - reduce friction with test helpers - 减少和测试助手的摩擦 - introducing spies - 引入侦探类 1. Test a Component 1. 测试组件 - test the component outside of Angular - 在Angular测试环境外测试组件 - mock the dependent asynchronous service - 模拟所依赖的异步服务 - simulate interaction with the view (no DOM) - 仿真和视图(没有DOM)的互动 - use a spy-promise to control asynchronous test flow - 使用侦探型承诺(spy-promise)来控制异步测试工作流 1. Test a Component in the DOM 1. 在DOM中测试组件 - test the component inside the Angular test environment - 在Angular测试环境中测试组件 - use the `TestComponentBuilder` - 使用`TestComponentBuilder` - more test helpers - 更多测试助手 - interact with the DOM - 与DOM互动 - bind to a mock dependent asynchronous service - 绑定到一个模拟的被依赖异步服务 1. Run the tests with karma 1. 用Karma运行这些测试 It’s a big agenda. Fortunately, you can learn a little bit at a time and put each lesson to use. 这是一个很大的日程表。幸运的是,我们可以一次只学一小点,然后把它们投入实战。