如果我在karma.conf.js中使用Chrome,我的业力单元测试工作正常,但如果更改为PhantomJS则失败

时间:2014-03-03 00:50:20

标签: angularjs phantomjs karma-runner

要测试的代码段:

$timeout(function() {

        $http.get('/getUpdates.json')
          .success(function(data, status, headers, config) {
          })
          .error(function(data, status, headers, config) {
          })
          .then(function(response) {
            if (usingPopOverWait) {
              $utilUi.hidePopOverWait();
            }
            var random = Math.floor((Math.random() * 4) + 1);
            if (random === 3) {
              deferred.reject('Error loading mock data');
            }
            else {
              deferred.resolve(response.data.success);
            }
          });
      }, 3000);

测试文件:

    describe('Controller: CtrlHelp', function () {

      beforeEach(module('nzrbTabApp'));

      var CtrlHelp, scope, rootScope, location, timeout, httpBackend, modelHelp;

      var mockResponseHelp =
      {
        'success' : {
          'hasUpdates': true
        },
        'error': {
        }
      };

      beforeEach(inject(function ($injector) {

        httpBackend = $injector.get('$httpBackend');
        timeout = $injector.get('$timeout');
        modelHelp = $injector.get('$modelHelp');
      }));

      beforeEach(inject(function ($controller, $rootScope, $location) {

        rootScope = $rootScope;
        scope = $rootScope.$new();
        location = $location;
        CtrlHelp = $controller('CtrlHelp', {
          $scope: scope,
          $location: location
        });
      }));

      it('Should load static content and attached to scope', function () {

        httpBackend.whenGET('/getUpdates.json').respond(mockResponseHelp);
        timeout.flush();
        httpBackend.flush();

        if (!scope.help.showError) {
         //check a condition
        }
      });

    });


    Error: No deferred tasks to be flushed

           at app/bower_components/angular-mocks/angular-mocks.js:121

           at app/bower_components/angular-mocks/angular-mocks.js:1659

           at test/spec/features/help/ctrl-help-test.js:91

在timeout.flush()行上抛出错误。使用Chrome作为浏览器时,相同的代码非常正常。

如果需要更多详细信息以帮助解决上述问题,请与我们联系。

此致

莫希特

1 个答案:

答案 0 :(得分:0)

这听起来像是一个时间问题,因为PhantomJS正在排空队列,但Chrome正在其中找到一个功能。

它也是known weirdnessngMock.$timeout的行为方式相同,将错误抛给空队列。解决方法是允许错误,必要时使用try-catch。

更新:以下代码适用于OS X上的PhantomJS 1.9.7(但我不知道哪个延迟对象deferred指的是。)所以听起来像问题可能与测试中的代码有关,而不是测试本身。

var nzrbTabApp = angular.module("nzrbTabApp", []);

nzrbTabApp.controller("CtrlHelp", function ($scope, $timeout, $http) {

    $timeout(function () {

        $http.get('/getUpdates.json')
            .success(function (data, status, headers, config) {
            })
            .error(function (data, status, headers, config) {
            })
            .then(function (response) {
                    $scope.error = "Error loading mock data";
            });
    }, 3000);
});

带测试

it('Should load static content and attached to scope', function () {

    httpBackend.whenGET('/getUpdates.json').respond(mockResponseHelp);
    timeout.flush();
    httpBackend.flush();

    expect(scope.error).toBeTruthy();
});