Almost every test has a pattern of visiting a route, interacting with the page (using the helpers), and checking for expected changes in the DOM.

几乎所有的测试都有访问路由的一种固有模式,就是与页面进行交互(通过助手),然后检测期待的改变是否在DOM中发生。

Examples:

例如:

  1. test('root lists first page of posts', function(){
  2. visit('/posts');
  3. andThen(function() {
  4. equal(find('ul.posts li').length, 3, 'The first page should have 3 posts');
  5. });
  6. });

The helpers that perform actions use a global promise object and automatically chain onto that promise object if it exists. This allows you to write your tests without worrying about async behaviour your helper might trigger.

这些助手使用一个全局的承诺来执行操作,如果这个承诺对象存在,会自动的链上这个对象。这也就不需要单向测试中助手可能触发的异步行为了。

  1. module('Integration: Transitions', {
  2. setup: function() {
  3. App.reset();
  4. }
  5. });
  6. test('add new post', function() {
  7. visit('/posts/new');
  8. fillIn('input.title', 'My new post');
  9. click('button.submit');
  10. andThen(function() {
  11. equal(find('ul.posts li:last').text(), 'My new post');
  12. });
  13. });

Live Example

在线示例

Testing User Interaction

测试用户交互

Testing Transitions

测试过渡

Suppose we have an application which requires authentication. When a visitor visits a certain URL as an unauthenticated user, we expect them to be transitioned to a login page.

假定有一个应用需要身份认证。当一位访问者最为未登录用户访问某一URL时,希望他被过渡到一个登录的页面。

  1. App.ProfileRoute = Ember.Route.extend({
  2. beforeModel: function() {
  3. var user = this.modelFor('application');
  4. if (Em.isEmpty(user)) {
  5. this.transitionTo('login');
  6. }
  7. }
  8. });

We could use the route helpers to ensure that the user would be redirected to the login page when the restricted URL is visited.

当受限的URL被访问时,可以通过路由助手确保用户被重定向到登录页面。

  1. module('Integration: Transitions', {
  2. setup: function() {
  3. App.reset();
  4. }
  5. });
  6. test('redirect to login if not authenticated', function() {
  7. visit('/');
  8. click('.profile');
  9. andThen(function() {
  10. equal(currentRouteName(), 'login');
  11. equal(currentPath(), 'login');
  12. equal(currentURL(), '/login');
  13. });
  14. });

Live Example

在线示例

Testing Transitions

测试过渡