diff --git a/public/docs/ts/latest/guide/testing.jade b/public/docs/ts/latest/guide/testing.jade
index 3dea48b2e8..4eb987f647 100644
--- a/public/docs/ts/latest/guide/testing.jade
+++ b/public/docs/ts/latest/guide/testing.jade
@@ -9,75 +9,122 @@ block includes
:marked
This chapter offers tips and techniques for testing Angular applications.
Along the way you will learn some general testing principles and techniques but the focus is on
- Angular testing.
+ testing applications written with Angular
- 本章提供了一些测试Angular应用的提示和技巧。虽然这里讲述了一些常规测试理念和技巧,但是其重点是Angular的测试。
+ 本章提供了一些测试Angular应用的提示和技巧。虽然这里讲述了一些常规测试理念和技巧,但是其重点是测试用Angular编写的应用。
-a#top
+#top
:marked
- # Contents
+ # Table of Contents
# 目录
- * [Introduction to Angular Testing](#testing-101)
+ 1. [Introduction to Angular Testing](#testing-intro)
- * [Angular测试入门](#testing-101)
+ 1. [Angular测试入门](#testing-intro)
+
- * [Setup](#setup)
+ 1. [Setup](#setup)
- * [搭建测试环境](#setup)
+ 1. [搭建测试环境](#setup)
+ - [setup files](#setup-files): `karma.conf`, `karma-test-shim`, `systemjs.config`
+ - [npm packages](#npm-packages)
- * [The first karma test](#1st-karma-test)
+ 1. [The first karma test](#1st-karma-test)
- * [第一个Karma测试](#1st-karma-test)
-
- * [The Angular Testing Platform (ATP) ](#atp-intro)
-
- * [Angular测试平台(ATP)](#atp-intro)
-
- * [The sample application and its tests](#sample-app)
-
- * [例子应用及其测试](#sample-app)
-
- * [A simple component test](#simple-component-test)
-
- * [一个简单的组件测试](#simple-component-test)
-
- * [Test a component with a service dependency](#component-with-dependency)
-
- * [测试拥有服务依赖的组件](#component-with-dependency)
-
- * [Test a component with an async service](#component-with-async-service)
+ 1. [第一个Karma测试](#1st-karma-test)
- * [测试拥有异步服务的组件](#component-with-async-service)
+
+
+ 1. [Introduction to the Angular testing utilities](#atu-intro)
+
+ 1. [Angular测试工具](#atu-intro)
- * [Test a component with an external template](#component-with-external-template)
-
- * [测试拥有外部模板的组件](#component-with-external-template)
-
- * [Test a component with inputs and outputs](#component-with-inputs-output)
-
- * [测试拥有导入(inputs)和导出(outputs)的组件](#component-with-inputs-output)
-
- * [Test a component inside a test host component](#component-inside-test-host)
-
- * [在宿主组件中测试组件](#component-inside-test-host)
-
- * [Test a routed component](#routed-component)
-
- * [测试路由组件](#routed-component)
-
- * [Isolated tests](#testing-without-atp "Testing without the Angular Testing Platform")
-
- * [隔离测试](#testing-without-atp "Testing without the Angular Testing Platform")
-
- * [_TestBed_ API](#atp-api)
-
- * [_TestBed_ API](#atp-api)
+
- * [FAQ](#faq "Frequently asked questions")
+ 1. [The sample application and its tests](#sample-app)
+
+ 1. [例子应用及其测试](#sample-app)
- * [常见问题](#faq "Frequently asked questions")
+
+
+ 1. [A simple component test](#simple-component-test)
+
+ 1. [一个简单的组件测试](#simple-component-test)
+
+ - [_configureTestingModule_](#configure-testing-module)
+ - [_createComponent_](#create-component)
+ - [_ComponentFixture_, _DebugElement_, _query(By.css)_](#component-fixture)
+ - [_detectChanges_](#detect-changes)
+ - [_autoDetectChanges_](#auto-detect-changes)
+
+ 1. [Test a component with a service dependency](#component-with-dependency)
+
+ 1. [测试拥有服务依赖的组件](#component-with-dependency)
+ - [test doubles](#service-test-doubles)
+ - [get the injected service](#get-injected-service)
+ - [_TestBed.get_](#testbed-get)
+
+ 1. [Test a component with an async service](#component-with-async-service)
+
+ 1. [测试拥有异步服务的组件](#component-with-async-service)
+
+ - [spies](#service-spy)
+ - [_async_](#async)
+ - [_whenStable_](#when-stable)
+ - [_fakeAsync_](#async)
+ - [_tick_](#tick)
+ - [_jasmine.done_](#jasmine-done)
+ 1. [Test a component with an external template](#component-with-external-template)
+
+ 1. [测试拥有外部模板的组件](#component-with-external-template)
+
+ - [_async_](#async-in-before-each) in `beforeEach`
+ - [_compileComponents_](#compile-components)
+ 1. [Test a component with inputs and outputs](#component-with-inputs-output)
+
+ 1. [测试拥有导入(inputs)和导出(outputs)的组件](#component-with-inputs-output)
+
+ - [_triggerEventHandler_](#trigger-event-handler)
+ 1. [Test a component inside a test host component](#component-inside-test-host)
+
+ 1. [在宿主组件中测试组件](#component-inside-test-host)
+
+
+ 1. [Test a routed component](#routed-component)
+
+ 1. [测试路由组件](#routed-component)
+
+ - [_inject_](#inject)
+ 1. [Test a routed component with parameters](#routed-component-w-param)
+ - [_Observable_ test double](#stub-observable)
+ 1. [Use a _page_ object to simplify setup](#page-object)
+
+ 1. [Setup with module imports](#import-module)
+
+ 1. [Override component providers](#component-override)
+
+ 1. [Test a _RouterOutlet_ component](#router-outlet-component)
+ - [stubbing unneeded components](#stub-component)
+ - [Stubbing the _RouterLink_](#router-link-stub)
+ - [_By.directive_ and injected directives](#by-directive)
+ 1. ["Shallow" component tests with *NO\_ERRORS\_SCHEMA*](#shallow-component-test)
+
+ 1. [Test an attribute directive](#attribute-directive)
+
+ 1. [Isolated unit tests](#isolated-unit-tests "Unit testing without the Angular testing utilities")
+ - [Services](#isolated-service-tests)
+ - [Pipes](#isolated-pipe-tests)
+ - [Components](#isolated-component-tests)
+ 1. [Angular testing utility APIs](#atu-apis)
+ - [Stand-alone functions](#atu-apis): `async`, `fakeAsync`, etc.
+ - [_TestBed_](#testbed-class-summary)
+ - [_ComponentFixture_](#component-fixture-class-summary)
+ - [_DebugElement_](#debug-element-details)
+
+ 1. [FAQ](#faq "Frequently asked questions")
+
+ 1. [常见问题](#faq "Frequently asked questions")
:marked
It’s a big agenda. Fortunately, you can learn a little bit at a time and put each lesson to use.
@@ -97,7 +144,7 @@ a(href="#top").to-top Back to top
a(href="#top").to-top 回到顶部
.l-hr
-a#testing-101
+#testing-intro
:marked
# Introduction to Angular Testing
@@ -163,28 +210,28 @@ table(width="100%")
它自带一个HTML测试运行器,用来在浏览器中执行测试。
tr(style=top)
- td(style="vertical-align: top") Angular测试平台
+ td(style="vertical-align: top") Angular测试工具
td
:marked
- The Angular Testing Platform creates a test environment and harness
- for the application code under test.
- Use it to condition and control parts of the application as they
+ The Angular testing utilities create a test environment
+ for the Angular application code under test.
+ Use them to condition and control parts of the application as they
interact _within_ the Angular environment.
- Angular测试平台创建一个测试环境,并为被测试的代码提供平台。在应用代码与Angular环境互动时,使用Angular测试平台来限制和控制应用的部分代码。
+ Angular测试工具为被测试的Angular应用代码创建一个测试环境。在应用代码与Angular环境互动时,使用Angular测试工具来限制和控制应用的部分代码。
tr(style=top)
td(style="vertical-align: top") Karma
td
:marked
The [karma test runner](https://karma-runner.github.io/1.0/index.html)
- is ideal for writing and running tests while developing the application.
- It can be an integral part of the application build process.
+ is ideal for writing and running unit tests while developing the application.
+ It can be an integral part of the project's development and continuous integration processes.
This chapter describes how to setup and run tests with karma.
[karma测试运行器](https://karma-runner.github.io/1.0/index.html)是在开发应用的过程中
- 编写和运行测试的理想工具。
- 它能成为应用编译过程中的一个不可分割的一部分。本章讲述了如何用Karma设置和运行测试。
+ 编写和运行单元测试的理想工具。
+ 它能成为项目开发和连续一体化进程的一个不可分割的一部分。本章讲述了如何用Karma设置和运行测试。
tr(style=top)
td(style="vertical-align: top") Protractor
@@ -200,7 +247,7 @@ table(width="100%")
在端对端测试中,一个进程运行真正的应用,另一个进程运行Protractor测试,模拟用户行为,判断应用在浏览器中的反应是否正确。
.l-hr
-a#setup
+#setup
:marked
# Setup
@@ -222,7 +269,7 @@ a#setup
1. Start a new project following the instructions in the
[QuickStart github repository](https://github.com/angular/quickstart/blob/master/README.md).
-
+
1. 根据[快速起步的github库](https://github.com/angular/quickstart/blob/master/README.md)中的说明创建一个新的项目.
1. Start a new project with the
@@ -248,11 +295,15 @@ a#setup
如果你的应用是基于《快速起步》库的,可以跳过本小节下面的内容,直接开始第一个测试。
《快速起步》库一同了所有必须的配置。
-
+
+#setup-files
:marked
+ ### Setup files
+
+ ### 文件配置
Here's brief description of the setup files.
- 下面是配置文件的简介:
+ 下面是文件配置的简介:
table(width="100%")
col(width="20%")
@@ -291,13 +342,13 @@ table(width="100%")
td
:marked
[SystemJS](https://github.com/systemjs/systemjs/blob/master/README.md)
- loads the application and test modules.
- This script tells SystemJS where to find the module files and how to load them.
- It's the same version of the file used by QuickStart-based applications.
+ loads the application and test files.
+ This script tells SystemJS where to find those files and how to load them.
+ It's the same version of `systemjs.config.js` used by QuickStart-based applications.
[SystemJS](https://github.com/systemjs/systemjs/blob/master/README.md)
- 加载应用和测试模块。本脚本告诉SystemJS去哪儿寻找模块文件,以及如何加载它们。
- 它的文件版本和基于《快速起步》的应用使用的一样。
+ 加载应用和测试文件。本脚本告诉SystemJS去哪儿寻找这些文件,以及如何加载它们。
+ `systemjs.config.js`的版本和基于《快速起步》的应用使用的一样。
tr
td(style="vertical-align: top") systemjs.config.extras.js
td
@@ -334,7 +385,7 @@ table(width="100%")
你运行`npm install`时就会安装它们。
.l-hr
-a#1st-karma-test
+#1st-karma-test
:marked
# The first karma test
@@ -372,64 +423,33 @@ a#1st-karma-test
## Run karma
## 运行`Karma`
- Compile and run it in karma from the command line.
-
- 从命令行中编译并在`Karma`中运行上面的测试配置文件。
-.l-sub-section
- :marked
- The QuickStart repo adds the following command to the `scripts` section in `package.json`.
-
- 《快速起步》库在`package.json`中的`scripts`里面添加了下列命令:
-
- code-example(format="." language="bash").
- "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
- :marked
- Add that to your `package.json` if it's not there already.
-
- 如果你的`package.json`里面没有上面的命令,那么就将上面的命令添加进去。
-
-:marked
- Open a terminal or command window and enter
-
- 打开一个终端或者命令窗口,输入:
+ Compile and run it in karma from the command line with this command:
+
+ 使用下面的命令从命令行中编译并在`Karma`中运行上面的测试配置文件。
+
code-example(format="." language="bash").
npm test
:marked
- The command compiles the application and test code a first time.
- If the compile fails, the command aborts.
-
- 上面的命令首先编译应用及其测试代码一次。如果编译出错,命令就会终止。
-
- If it succeeds, the command re-compiles (this time in watch mode) in one process
- and starts karma in another.
- Both processes watch pertinent files and re-run when they detect changes.
-
- 如果编译成功,该命令则一个进程中,在监视模式下再次编译,并在另一个进程中启动`Karma`。
- 两个进程都监视相关文件,并在发现变化时自动再次运行。
-
- After a few moments, karma opens a browser ...
-
- 过一会儿,`Karma`便打开浏览器...
+ The command compiles the application and test code and starts karma.
+ Both processes watch pertinent files, write messages to the console, and re-run when they detect changes.
+.l-sub-section
+ :marked
+ The QuickStart development path defined the `test` command in the `scripts` section of npm's `package.json`.
+ The Angular CLI has different commands to do the same thing. Adjust accordingly.
+:marked
+ After a few moments, karma opens a browser and starts writing to the console.
figure.image-display
img(src='/resources/images/devguide/testing/karma-browser.png' style="width:400px;" alt="Karma browser")
:marked
- ... and starts writing to the console.
-
- ... 并开始在控制台输入信息。
-
Hide (don't close!) the browser and focus on the console output which should look something like this.
隐藏(不要关闭)浏览器,查看控制台的输出,应该看起来像这样:
code-example(format="." language="bash").
> npm test
- > tsc && concurrently "tsc -w" "karma start karma.conf.js"
-
+ ...
[0] 1:37:03 PM - Compilation complete. Watching for file changes.
- [1] 24 07 2016 13:37:09.310:WARN [karma]: No captured browser, open http://localhost:9876/
- [1] 24 07 2016 13:37:09.361:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
- [1] 24 07 2016 13:37:09.370:INFO [launcher]: Starting browser Chrome
- [1] 24 07 2016 13:37:10.974:INFO [Chrome 51.0.2704]: Connected on socket /#Cf6A5PkvMzjbbtn1AAAA with id 24600087
+ ...
[1] Chrome 51.0.2704: Executed 0 of 0 SUCCESS
Chrome 51.0.2704: Executed 1 of 1 SUCCESS
SUCCESS (0.005 secs / 0.005 secs)
@@ -458,8 +478,7 @@ code-example(format="." language="bash").
**`Karma`**监视器检测到编译器输出的变化,并重新运行测试。
code-example(format="." language="bash").
- [1] Chrome 51.0.2704: Executed 0 of 1 SUCCESS
- Chrome 51.0.2704 1st tests true is true FAILED
+ [1] Chrome 51.0.2704 1st tests true is true FAILED
[1] Expected false to equal true.
[1] Chrome 51.0.2704: Executed 1 of 1 (1 FAILED) (0.005 secs / 0.005 secs)
@@ -497,14 +516,15 @@ code-example(format="." language="bash").
在浏览器中,像调试应用一样调试测试配置。
- Reveal the karma browser window (hidden earlier).
-
- 显示`Karma`的浏览器窗口(之前被隐藏了)。
-
+
+ - Click the "DEBUG" button; it opens a new browser tab and re-runs the tests
+
- Open the browser's “Developer Tools” (F12 or Ctrl-Shift-I).
- 打开浏览器的“Developer Tools”(F12或者Ctrl-Shift-I)。
- - Pick the “sources” section
+ - Pick the "sources" section
- 选择“sources”页
@@ -527,125 +547,93 @@ a(href="#top").to-top Back to top
a(href="#top").to-top 回到顶部
.l-hr
-a#atp-intro
+#atu-intro
:marked
- # The Angular Testing Platform (ATP)
-
- # Angular测试平台(ATP)
+ # Introduction to the Angular Testing Utilities
Many tests explore how applications classes interact with Angular and the DOM while under Angular's control.
许多测试探索应用的类在被`Angular`控制时,是如何与`Angular`和`DOM`互动的。
- Such tests are easy to write with the help of the _Angular Testing Platform_ (ATP)
- which consists of the `TestBed` class and some helper functions.
+ Such tests are easy to write with the help of the Angular testing utilities
+ which include the `TestBed` class and some helper functions.
- **Angular测试平台**(ATP)有`TestBed`类和一些助手函数方法,在它们的帮助下,很容易编写上面那样的测试。
+ Angular测试工具包含了`TestBed`类和一些助手函数方法,在它们的帮助下,很容易编写上面那样的测试。
- Tests written with the _Angular Testing Platform_ are the main focus of this chapter.
+ Tests written with these utilities are the main focus of this chapter.
But they are not the only tests you should write.
- 利用**Angular测试平台**编写的测试是本章的主要焦点。但是它们不是你能写的唯一测试类型。
+ 利用**这些工具**编写的测试是本章的主要焦点。但是它们不是你能写的唯一测试类型。
### Isolated unit tests
### 孤立的单元测试
- You can and should write [isolated unit tests](#testing-without-atp "Testing without the Angular Testing Platform")
- for components, directives, pipes, and services.
- Isolated unit tests examine an instance of a class all by itself without
- any dependence on Angular or any injected values.
- The tester creates a test instance of the class with new, supplying fake constructor parameters as needed, and
+ [Isolated unit tests](#isolated-unit-tests "Unit testing without the Angular testing utilities")
+ examine an instance of a class all by itself without any dependence on Angular or any injected values.
+ The tester creates a test instance of the class with new, supplying test doubles for the constructor parameters as needed, and
then probes the test instance API surface.
-
- 你能,也应该为组件、指令、管道和服务等编写[孤立的单元测试](#testing-without-atp "Testing without the Angular Testing Platform")。
- 孤立的单元测试独立自己检测类的实例,不依靠Angular或者任何其他注入值。
- 测试器使用`new`创建一个类的测试实例,在需要时提供伪造的构造函数参数,并测试被测试实例的API。
-
- Isolated tests don't reveal how the class interacts with Angular.
+
+ [孤立的单元测试](#isolated-unit-tests "不使用Angular测试工具进行单元测试")独立自己检测类的实例,不依靠Angular或者任何其他注入值。
+ 测试器使用`new`创建一个类的测试实例,在需要时提供用于测试的构造函数参数复制品,并测试被测试实例的API。
+
+ You can and should write isolated unit tests for pipes and services.
++makeExample('testing/ts/app/shared/title-case.pipe.spec.ts', 'mini-excerpt', 'app/shared/title-case.pipe.spec.ts (excerpt)')
+:marked
+ Components can be tested in isolation as well.
+ However, isolated unit tests don't reveal how these classes interact with Angular.
In particular, they can't reveal how a component class interacts with its own template or with other components.
+ Such tests require the Angular testing utilities.
孤立的测试不会展示类是如何与Angular互动的。也就是说,它们不会展示组件的类是如何与自己的模板或者其他组件互动的。
+ 这样的测试需要Angular测试工具。
- Those tests require the Angular Testing Platform.
+ ### Testing with the Angular Testing Utilities
- 那些互动的测试需要Angular测试平台。
+ ### 利用**Angular测试工具**进行测试
- ### Testing with the _ Angular Testing Platform_
+ The Angular testing utilities include the `TestBed` class and several helper functions from `@angular/core/testing`.
- ### 利用**Angular测试平台**测试
+ **Angular测试工具**包含了`TestBed`类和在`@angular/core/testing`中一些助手函数方法。
- The _Angular Testing Platform_ consists of the `TestBed` class and some helper functions from `@angular/core/testing`.
-
- **Angular测试平台**包含了`TestBed`类和在`@angular/core/testing`中一些助手函数方法。
-
-.alert.is-important
- :marked
- The _TestBed_ is officially _experimental_ and thus subject to change.
- Consult the [API reference](../api/core/testing/index/TestBed-class.html) for the latest status.
-
- **TestBed**目前还处于**实验阶段**,所以有可能变化。
- 到[API参考](../api/core/testing/index/TestBed-class.html)查看最新动态。
-:marked
- The `TestBed` creates an Angular test module — an `@NgModule` class —
+ The `TestBed` creates an Angular testing module — an `@NgModule` class —
that you configure to produce the module environment for the class you want to test.
- You tell the `TestBed` to create an instance of the test component and probe that instance with tests.
+ You tell the `TestBed` to create an instance of the _component-under-test_ and probe that instance with tests.
`TestBed`创建一个Angular测试模块 - 一个`@NgModule`类 - 通过配置它,你为想要测试的类创造模块环境。
通过`TestBed`创建一个被测试的组件的实例,并使用测试来测探这个实例。
-
- That's the `TestBed` in a nutshell.
-
- 大概的说,这就是`TestBed`的。
-
- In practice, you work with the static methods of the `TestBed` class.
- These static methods create and update a fresh hidden `TestBed` instance before each Jasmine `it`.
-
- 实际应用中,你调用`TestBed`类的静态方法。在每个Jasmine的`it`之前,这些静态方法创建和更新一个全新隐式`TestBed`实例。
+
+ Before each spec, the `TestBed` resets itself to a base state.
+ The base state includes a default testing module configuration consisting of the
+ declarables (components, directives, and pipes) and providers (some of them mocked)
+ that almost everyone needs.
.l-sub-section
:marked
- You can access that hidden instance anytime by calling `getTestBed()`;
-
- 你可以随时访问这个隐式实例,调用`getTestBed()`即可。
-
+ The testing shims mentioned [earlier](#setup) initialize the testing module configuration
+ to something like the `BrowserModule` from `@angular/platform-browser`.
:marked
- This `TestBed` instance comes pre-configured with a baseline of default providers and declarables (components, directives, and pipes)
- that almost everyone needs.
- This chapter tests a browser application so the default includes the `CommonModule` declarables from `@angular/common`
- and the `BrowserModule` providers (some of them mocked) from `@angular/platform-browser`.
+ This default configuration is merely a _foundation_ for testing an app.
+ You call `TestBed.configureTestingModule` with an object that defines additional imports, declarations, providers and schemas
+ to fit your application tests.
+ Optional `override...` methods can fine-tune aspects of the configuration.
- 这个`TestBed`实例自带预先配置好的、几乎所有人都需要的默认提供商、可声明类(组件、指令和管道)。
- 本章测试一个浏览器应用程序,所以默认包含`@angular/common`中的`CommonModule`可声明类,以及`@angular/platform-browser`中的`BrowserModule`服务提供商(其中一些是Mocked)。
-
- You refine the default test module configuration with application and test specifics
- so that it can produce an instance of the test component in the Angular environment suitable for your tests.
-
- 通过定义默认的测试模块配置为应用和测试特有特性,你可以为你的测试在Angular环境中生成一个测试组件的实例。
-
- Start by calling `TestBed.configureTestingModule` with an object that looks like `@NgModule` metadata.
- This object defines additional imports, declarations, providers and schemas.
-
- 首先,用一个类似与`@NgModule`元数据的对象来调用`TestBed.configureTestingModule`。这个对象定义了额外的imports、declarations、providers和schemas。
-
- After configuring the `TestBed`, tell it to create an instance of the test component and the test fixture
- you'll need to inspect and control the component's immediate environment.
-
- 然后配置`TestBed`,告诉它创建一个测试组件的实例和测试工具,利用它们,你可以检测和控制组件的周围环境。
+ After configuring the `TestBed`, tell it to create an instance of the _component-under-test_ and the test fixture
+ that you'll need to inspect and control the component's immediate environment.
+makeExample('testing/ts/app/banner.component.spec.ts', 'simple-example-before-each', 'app/banner.component.spec.ts (simplified)')(format='.')
:marked
Angular tests can interact with the HTML in the test DOM,
simulate user activity, tell Angular to perform specific task (such as change detection),
- and see the effects of these actions both in the test component and in the test DOM.
+ and see the effects of these actions both in the _component-under-test_ and in the test DOM.
- Angular测试可以在测试DOM中与HTML互动,模拟用户行为,告诉Angular执行特定任务(比如变换检测),并在测试组件和测试DOM中查看这些行为的效果。
+ Angular测试可以在测试DOM中与HTML互动,模拟用户行为,告诉Angular执行特定任务(比如变换检测),并在被测试的组件和测试DOM中查看这些行为的效果。
+makeExample('testing/ts/app/banner.component.spec.ts', 'simple-example-it', 'app/banner.component.spec.ts (simplified)')(format='.')
:marked
- A comprehensive review of the _TestBed_ API appears [later in the chapter](#atp-api).
- Let's dive right into Angular testing, starting with the components of a sample application.
+ A comprehensive review of the Angular testing utilities appears [later in the chapter](#atu-apis).
+ Let's dive right into Angular testing, starting with with the components of a sample application.
- 完整的关于**TestBed**的回顾将会在[本章后面](#atp-api)出现。
+ 完整的关于Angular测试工具的回顾将会在[本章后面](#atu-apis)出现。
让我们深入到Angular测试,以一个例子应用的组件开始。
a(href="#top").to-top Back to top
@@ -653,7 +641,7 @@ a(href="#top").to-top 回到顶部
.l-hr
-a#sample-app
+#sample-app
:marked
# The sample application and its tests
@@ -677,9 +665,9 @@ a#sample-app
It includes the tests discussed in this chapter and additional tests for you to explore.
This live example contains both application and test code.
- It is large and can take several minutes to start. Please be patient.
+ Give it some time to load and warm up.
- 它包含了本章讨论的测试和其他测试。本在线例子包含了整个应用和测试代码。它比较大,可能需要好几分钟才能启动,请耐心等待。
+ 它包含了本章讨论的测试和其他测试。本在线例子包含了整个应用和测试代码。给它一些时间来加载。
fakeAsync
td
:marked
Runs the body of a test (`it`) within a special _fakeAsync test zone_, enabling
- a linear control flow coding style. See [above](#fake-async).
+ a linear control flow coding style. See [discussion above](#fake-async).
tr
td(style="vertical-align: top") tick
td
:marked
Simulates the passage of time and the completion of pending asynchronous activities
- by flushing timer and micro-task queues in the _fakeAsync test zone_.
-
+ by flushing both _timer_ and _micro-task_ queues within the _fakeAsync test zone_.
+
+ .l-sub-section
+ :marked
+ The curious, dedicated reader might enjoy this lengthy blog post,
+ "_Tasks, microtasks, queues and schedules_".
+ :marked
Accepts an optional argument that moves the virtual clock forward
the specified number of milliseconds,
clearing asynchronous activities scheduled within that timeframe.
- See [above](#tick).
+ See [discussion bove](#tick).
tr
td(style="vertical-align: top") inject
@@ -1726,22 +2236,22 @@ table
td(style="vertical-align: top") discardPeriodicTasks
td
:marked
- When a `fakeAsync` test ends with pending timer event tasks (queued `setTimeOut` and `setInterval` callbacks),
+ When a `fakeAsync` test ends with pending timer event _tasks_ (queued `setTimeOut` and `setInterval` callbacks),
the test fails with a clear error message.
In general, a test should end with no queued tasks.
- When pending timer tasks are expected, call `discardPeriodicTasks` to flush the queues
+ When pending timer tasks are expected, call `discardPeriodicTasks` to flush the _task_ queue
and avoid the error.
tr
td(style="vertical-align: top") flushMicrotasks
td
:marked
- When a `fakeAsync` test ends with pending "microtasks" such as unresolved promises,
+ When a `fakeAsync` test ends with pending _micro-tasks_ such as unresolved promises,
the test fails with a clear error message.
- In general, a test should wait for microtasks to finish.
- When pending microtasks are expected, call `discardPeriodicTasks` to flush the queues
+ In general, a test should wait for micro-tasks to finish.
+ When pending microtasks are expected, call `flushMicrotasks` to flush the _micro-task_ queue
and avoid the error.
tr
@@ -1762,19 +2272,14 @@ table
.l-hr
-a#testbed-class-summary
+#testbed-class-summary
:marked
# _TestBed_ Class Summary
- The `TestBed` class API is quite large and can be overwhelming until you've explored it first
+ The `TestBed` class is one of the principal Angular testing utilities.
+ Its API is quite large and can be overwhelming until you've explored it first
a little at a time. Read the early part of this chapter first
to get the basics before trying to absorb the full API.
-.alert.is-important
- :marked
- The _TestBed_ is officially _experimental_ and thus subject to change.
- Consult the [API reference](../api/core/testing/index/TestBed-class.html) for the latest status.
-
-:marked
The module definition passed to `configureTestingModule`,
is a subset of the `@NgModule` metadata properties.
code-example(format="." language="javascript").
@@ -1785,6 +2290,7 @@ code-example(format="." language="javascript").
schemas?: Array<SchemaMetadata | any[]>;
};
+#metadata-override-object
:marked
Each overide method takes a `MetadataOverridecompileComponents
td
:marked
- Compile the test module asynchronously after you've finished configuring it.
- You **must** call this method if _any_ of the test module components have a `templateUrl`
+ Compile the testing module asynchronously after you've finished configuring it.
+ You **must** call this method if _any_ of the testing module components have a `templateUrl`
or `styleUrls` because fetching component template and style files is necessarily asynchronous.
See [above](#compile-components).
@@ -1843,7 +2349,7 @@ table
td
:marked
Replace metadata for the given `NgModule`. Recall that modules can import other modules.
- The `overrideModule` method can reach deeply into the current test module to
+ The `overrideModule` method can reach deeply into the current testing module to
modify one of these inner modules.
tr
td(style="vertical-align: top") overrideComponent
@@ -1896,20 +2402,20 @@ table
This method may be called _exactly once_. Call `resetTestEnvironment` first
if you absolutely need to change this default in the middle of your test run.
- Specify the Angular compiler factory, a `PlatformRef`, and a default Angular test module.
- Test modules and platforms for individual platforms are available from
- `angular2/platform/testing/resetTestEnvironment
td
:marked
- Reset the initial test environment including the default test module.
+ Reset the initial test environment including the default testing module.
:marked
A few of the `TestBed` instance methods are not covered by static `TestBed` _class_ methods.
These are rarely needed.
-a#componentfixture-api-summary
+#component-fixture-api-summary
:marked
## The _ComponentFixture_
@@ -1920,7 +2426,7 @@ a#componentfixture-api-summary
The `ComponentFixture` properties and methods provide access to the component,
its DOM representation, and aspects of its Angular environment.
-a#componentfixture-properties
+#component-fixture-properties
:marked
### _ComponentFixture_ properties
@@ -1942,7 +2448,7 @@ table
The `DebugElement` associated with the root element of the component.
The `debugElement` provides insight into the component and its DOM element during test and debugging.
- It's a critical property for testers. The most interesting members are covered [below](#debugelement-details).
+ It's a critical property for testers. The most interesting members are covered [below](#debug-element-details).
tr
td(style="vertical-align: top") nativeElement
td
@@ -1958,7 +2464,7 @@ table
component that has the `ChangeDetectionStrategy.OnPush`
or the component's change detection is under your programmatic control.
-a#componentfixture-methods
+#component-fixture-methods
:marked
### _ComponentFixture_ methods
@@ -2025,7 +2531,7 @@ table
:marked
Trigger component destruction.
-a#debugelement-details
+#debug-element-details
:marked
### _DebugElement_
@@ -2034,11 +2540,6 @@ a#debugelement-details
From the test root component's `DebugElement`, returned by `fixture.debugElement`,
you can walk (and query) the fixture's entire element and component sub-trees.
-.alert.is-important
- :marked
- The _DebugElement_ is officially _experimental_ and thus subject to change.
- Consult the [API reference](../api/core/index/DebugElement-class.html) for the latest status.
-:marked
Here are the most useful `DebugElement` members for testers in approximate order of utility.
table
@@ -2112,8 +2613,10 @@ table
:marked
Triggers the event by its name if there is a corresponding listener
in the element's `listeners` collection.
-
- If the event lacks a listner or there's some other problem,
+ The second parameter is the _event object_ expected by the handler.
+ See [above](#trigger-event-handler).
+
+ If the event lacks a listener or there's some other problem,
consider calling `nativeElement.dispatchEvent(eventObject)`
tr
@@ -2141,7 +2644,7 @@ table
Dictionary of objects associated with template local variables (e.g. `#foo`),
keyed by the local variable name.
-a#query-predicate
+#query-predicate
:marked
The `DebugElement.query(predicate)` and `DebugElement.queryAll(predicate)` methods take a
predicate that filters the source element's subtree for matching `DebugElement`.
@@ -2158,7 +2661,7 @@ a#query-predicate
+makeExample('testing/ts/app/hero/hero-list.component.spec.ts', 'by', 'app/hero/hero-list.component.spec.ts')(format=".")
-a#renderer-tests
+#renderer-tests
:marked
Many custom application directives inject the `Renderer` and call one of its `set...` methods.
@@ -2206,7 +2709,7 @@ a(href="#top").to-top Back to top
.l.hr
- a#faq
+ #faq
.l-main-section
:marked
## FAQ: Frequently Asked Questions
@@ -2215,7 +2718,7 @@ a(href="#top").to-top Back to top
General
* [When are end-to-end (e2e) tests a good choice?](#q-when-e2e)
* [When to use the _TestBed_?](#q-why-testbed)
- * [When to write vanilla tests without the _TestBed_?](#q-when-no-testbed)
+ * [When to write isolated unit tests without the _TestBed_?](#q-when-no-testbed)
* [When can I skip _TestBed.compileComponents_?](#q-when-no-compile-components)
* [Why must _TestBed.compileComponents_ be called last?](#q-why-compile-components-is-last)
* [Why must _inject_ be called last?](#q-why-last-last)
@@ -2244,7 +2747,7 @@ a(href="#top").to-top Back to top
.l-hr
-a#q-spec-file-location
+#q-spec-file-location
:marked
### Why put specs next to the things they test?
@@ -2258,7 +2761,7 @@ a#q-spec-file-location
.l-hr
-a#q-specs-in-test-folder
+#q-specs-in-test-folder
:marked
### When would I put specs in a test folder?