然后测试然后从角度的承诺中捕获

时间:2017-03-26 12:45:17

标签: angularjs jasmine

我想从我的$ scope.customerinfo测试我的then和catch函数。问题是我不知道究竟是怎么回事。

    var app = angular.module('shop', ['ngRoute','ngResource'])
      .factory('Customerservice', function ($resource) {
          return $resource('http://localhost:8080/Shop/:customer',{customer: "@customer"});
    })
      .controller('customerController', function ($scope,Customerservice) {
         $scope.customerinfo = CustomerService.get({customer: "Mark"});
         $scope.customerinfo.$promise.then(function(info) {
            return info;
         }).catch(function(errorResponse) {
            throw errorResponse;
         });
});

我还没有完成,但这是我的茉莉花代码

describe('Testing the customerinfo', function () {
  var $scope;
  var $q;
  var deferred;

  beforeEach(module('shop'));

  beforeEach(inject(function($controller, _$rootScope_, _$q_) {
    $q = _$q_;
    $scope = _$rootScope_.$new();
    deferred = _$q_.defer();
    $controller('userController', {
      $scope: $scope
    });
    }));
    it('should reject promise', function () {
   // I want to check if the catch option is working
  });
});

那么我究竟能做到这一点,还是需要重构代码呢?

1 个答案:

答案 0 :(得分:1)

茉莉花'它' method接受一个可以调用异步测试的完成参数

it('Should reject', function(done) {
  someAsyncFunction().catch(function(result) {
    expect(result.status).toBe(401);
    done();
  });
});