为Ember Test传递错误,即401,403等

时间:2016-05-03 17:17:57

标签: ruby-on-rails ember.js

在Ember测试中搜索有关处理错误的建议?从去年Allowing rejected promises in tests发现了一个封闭的问题,表明不能方便地处理拒绝问题。这仍然是这样吗?

现在,在我们的应用程序中,如果用户从我们的主Rails应用程序注销,我们使用ajax错误来返回未经授权的错误。

路线

import Ember from 'ember';
import {isUnauthorizedError, isForbiddenError} from 'ember-ajax/errors';

export default Ember.Route.extend({
  userService: Ember.inject.service('user'),

  activate: function(){

    this.get('userService').facility().then(
      (data) => {
        this.get('controller').set('facility', data.data);
      }, (response) => {
        if (isUnauthorizedError(response)) {
          this.transitionTo('report.failure.401');
        } else if (isForbiddenError(response)) {
          this.transitionTo('report.failure.403');
        } else {
        }
      }
    ),

验收测试

test('generates 401 error', function(assert) {
  visit('/reporting');

  httpStubs.stubUser(server, {}, 401);
  httpStubs.stubFacility(server, {});

  httpStubs.stubReport(server, '/api/reports/summary.json', []);
  click('button.btn-filter')

  andThen(function(){
    assert.equal(currentURL(), '/users/login');
  });

报告存根

  stubReport(server, url, data, status) {
    let statusCode = (status) ? status : 200;

    server.get(url, function() {

      return [
        statusCode,
        {'Content-Type': 'application/json'},
        JSON.stringify(data)
      ];
    });
  }

0 个答案:

没有答案