0

在这个 Angular/Jasmine 示例中,我有一个带有 promise 和$timeout.

测试失败,因为在 中设置的变量$timeout未定义。其他没有设置在里面的变量没有$timeout这个问题。

只有这个expect失败,其他的工作:

expect($scope.notes).toBe(notes);

我曾经$timeout.flush()等待,但它被忽略了。任何想法如何解决这一问题?

PLUNK http://plnkr.co/edit/Jet3KRs7baTIzk8L30JQ

angular.module("mymodule", [])

.service('myHttp', function($http){
    this.call = function($q) {
        var defer = $q.defer();
        $http({url:"gothere"})
              .then(function (response) {
                  defer.resolve(response);
              });
        return defer.promise;
    };
})

.controller('ctl', function($scope,$timeout,myHttp) {
      $scope.read = function (id){
          var data = {};
          data.id = id;
          myHttp.call({url:'/getRecord', data:data})
              .then(function(response) {
                  $scope.id = response.id;
                  $scope.name = response.nm;
                  $scope.descrip = response.dsc;

                  $timeout(function(){
                      $scope.notes = response.nts;
                  },1000);

              });
    };
});



describe('Testing a Controller that uses a Promise', function () {
  var $scope;
  var $q;
  var deferred;

  beforeEach(module('mymodule'));

  beforeEach(inject(function($controller, _$rootScope_, _$q_, $timeout, myHttp) {
    $scope = _$rootScope_.$new();
    $q = _$q_;
    deferred = $q.defer();
    spyOn(myHttp, 'call').and.returnValue(deferred.promise);

    $controller('ctl', { 
      $scope: $scope, 
      $timeout: $timeout,
      myHttp: myHttp
    });

    $scope.read(1)
    $timeout.flush(2000);
  }));

  it('should resolve promise', function () {

    var id = 1;
    var name = "John";
    var descrip = "This is the description";
    var notes = "These are the notes";

    var obj = {
      id: id,
      nm: name,
      dsc: descrip,
      nts: notes
    };

    deferred.resolve(obj);
    $scope.$apply();
    expect($scope.id).toBe(id);
    expect($scope.name).toBe(name);
    expect($scope.descrip).toBe(descrip);
    expect($scope.notes).toBe(notes);
  });

});
4

2 回答 2

0

给一个超时来读取变量:

 it("should be proper after timeout", function(){
      expect($scope.notes).toBe(notes);  
 }, 1000)
于 2017-01-11T17:39:36.703 回答
0

答案是:在 $scope.apply(); 之后使用 $timeout.flush();

 $scope.$apply();
 $timeout.flush();
于 2017-01-17T22:32:20.257 回答