未加星标

Essential Angular: Testing

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Essential Angular: Testing

One of the design goals of Angular is to make testing easy. That’s why the framework relies on dependency injection, separates the user code from the framework code, and comes with a set of tools for writing and running tests. In this article I will look at four ways to test Angular components: isolated tests, shallow tests, integration tests, and protractor tests.

Read theSeries

This is the sixth post in the Essential Angular series, which aims to be a short, but at the same time, fairly complete overview of the key aspects of Angular. Even though it’s not required, I recommend to read the first five posts in the series before starting on this one.

Essential Angular. Part 1: Compilation Essential Angular. Part 2: NgModules Essential Angular. Part 3: Components and Directives Essential Angular. Part 4: Dependency Injection Essential Angular. Part 5: Change Detection Example App

Throughout this series I use the same application in the examples. This application is a list of tech talks that you can filter, watch, and rate.


Essential Angular: Testing

You can find the source code of the application here .

Isolated Tests

It is often useful to test complex components without rendering them. To see how it can be done, let’s write a test for the following component:

filters.component.html:

There a few things in this example worth noting:

We are using reactive forms in the template of this component. This require us to manually create a form object in the component class, which has a nice consequence: we can test input handling without rendering the template. We listen to all the form changes, debounce them using the RxJS debounceTime operator, and then emit a change event. Finally, we inject a function to create a filters object out of the form.

Now, let’s look at the test.

As you can see, testing Angular components in isolation is no different from testing any other javascript object. We do not use any Angular UI-specific utilities.We, however, use fakeAsync. This is a utility provided by zone.js and using it we can control time, which is handy for testing the debouncing. Also, this test does not exercise the template of this component. The template might as well be empty ― the test will still pass.

Shallow Testing

Testing component classes without rendering their templates works in certain scenarios, but not in all of them. Sometimes we can write a meaningful test only if we render a component’s template. We can do that and still keep the test isolated. We just need to render the template without rendering the component’s children. This is what is colloquially known as shallow testing.

Let’s see this approach in action.

This component simply renders a collection of TalkCmp.

Now let’s look at its test.

First, look at how we configured our testing module. We only declared TalksCmp, nothing else. This means that all the elements in the template will be treated as simple DOM nodes, and only common directives (e.g., ngIf and ngFor) will be applied. This is exactly what we want. Second, passing NO_ERRORS_SCHEMA tells the compiler not to error on unknown elements and attributes, which is what we need for shallow tests. The result will create a list of talk-cmp DOM elements, which we inspected in the test. No instances of TalkCmp were created.

Integration Testing

We can also write an integration test that will exercise the whole application.

Note here we are importing AppModule, which means that Angular will create all the registered provides and will compile all the registered components. The test itself is self explanatory.

Even though both the shallow and integration tests render components, these tests are very different in nature.In the shallow test we mock up every single dependency of a component, and we do not render any of the component’s children. The goal is to exercise one slice of the component tree in isolation. In the integration tests we mock up only platform dependencies (e.g, location), and we use production code for the rest. Shallow tests are isolated, and, as a result, can be used to drive the design of our components. Integration tests are only used to check the correctness.

Protractor Tests

Finally, we can always write a protractor test exercising the whole application.

First, we created a page object, which is a good practice for making tests more domain-centric, so they talk more about user stories and not the DOM. Second, we wrote a protractor test verifying that filtering by title works.

Both protractor tests and integration tests (as defined above), solve the same problem ― they verify correctness, i.e., they verify that a particular use case have been implemented. Which ones should we use? I tend to test most of the behavior using integration tests, and use protractor only for a few smoke tests, but it is highly dependent on the culture of the team.

Let’s Recap

In this article we looked at four ways to test Angular components: isolated tests, shallow tests, integration tests, and protractor tests. Each of them have their time and place: isolated tests are a great way to test drive your components and test complex logic. Shallow tests are isolated tests on steroids, and they should be used when writing a meaningful test requires to render a component’s template. Finally, integration and protractor tests verify that a group of components and services (i.e., the application) work together.

Essential AngularBook

This article is based on the Essential Angular book, which you can find here https://leanpub.com/essential_angular . If you enjoy the article, check out the book!

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptJava
分页:12
转载请注明
本文标题:Essential Angular: Testing
本站链接:http://www.codesec.net/view/523154.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(73)