$ httpBackend.flush();导致卡玛失败

时间:2014-10-27 19:39:25

标签: angularjs karma-runner karma-jasmine

当我运行grunt jasmine我的所有规格都通过时,但当我运行grunt karma时,因为“警告:任务'业力:单位'失败,因此基本无声地失败。使用--force继续。中止到期警告。“告诉我几乎没有。如果我删除了所有$ httpBackend检查,则grunt karma再次通过。当我添加flush()方法时,几乎似乎会发生这种情况。

Angular和Angular-Mocks都在1.2.26。

有谁知道为什么我的单元测试在Karma中失败而在Jasmine中失败?

'use strict';

describe('DashboardCtrl spec', function() {

  var $scope,
      $route,
      $controller,
      $httpBackend;

  // Load the services's module
  beforeEach(module('LocalStorageModule'));
  beforeEach(module('myApp'));

  beforeEach(inject(function(_$controller_, $rootScope, _$route_, _$httpBackend_) {
    $scope = $rootScope.$new();
    $route = _$route_;
    $httpBackend = _$httpBackend_;
    $controller = _$controller_;

    $httpBackend.expectGET('//localhost:8081/api1/api/things1/').respond({});
    $httpBackend.expectGET('//localhost:8081/api1/api/things2/').respond({
      'next': 'somepage.html'
    });
    $httpBackend.expectGET('views/dashboard.html').respond('<p></p>');
    $controller('DashboardCtrl', {$scope: $scope, $route: { current: { params: ''}}});
    $httpBackend.flush();
    $scope.$digest();
  }));

  describe('$scope.init()', function() {

    it('Check $scope assignments.', function() {
      expect($scope.displayLoadContainer).toBe(true);
    });

  });

});



// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2014-09-16 using
// generator-karma 0.8.3

module.exports = function(config) {
  'use strict';

  config.set({
    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // base path, that will be used to resolve files and exclude
    basePath: '../',

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks: ['jasmine'],

    preprocessors: {
      'app/scripts/controllers/dashboard.js': 'coverage'
    },

    // list of files / patterns to load in the browser
    files: [
      'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'bower_components/angular-animate/angular-animate.js',
      'bower_components/angular-cookies/angular-cookies.js',
      'bower_components/angular-resource/angular-resource.js',
      'bower_components/angular-route/angular-route.js',
      'bower_components/angular-sanitize/angular-sanitize.js',
      'bower_components/angular-touch/angular-touch.js',
      'bower_components/tp-utils/app/scripts/services/tp-utils.js',
      'bower_components/angular-local-storage/angular-local-storage.js',
      'bower_components/ngInfiniteScroll/build/ng-infinite-scroll.js',
      'bower_components/tp-services/dist/tp-services.js',

      'app/scripts/app.js',
      'app/scripts/api-dev.js',
      'app/scripts/controllers/dashboard.js',

      'test/spec/**/*.js'
    ],

    // list of files / patterns to exclude
    exclude: [],

    // web server port
    port: 8080,

    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    browsers: [
      'PhantomJS'
    ],

    // Which plugins to enable
    plugins: [
      'karma-phantomjs-launcher',
      'karma-jasmine',
      'karma-coverage'
    ],

    reporters: ['coverage'],
    coverageReporter: {
      type : 'text',
      dir : 'coverage/'
    },

    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false,

    colors: true,

    // level of logging
    // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
    logLevel: config.LOG_INFO,

    // Uncomment the following lines if you are using grunt's server to run the tests
    // proxies: {
    //   '/': 'http://localhost:9000/'
    // },
    // URL root prevent conflicts with the site root
    // urlRoot: '_karma_'
  });
};

1 个答案:

答案 0 :(得分:3)

我担心您的单元测试是错误的。您无法在beforeEach函数中获得期望。我认为你想要做的是这样的事情:

describe('DashboardCtrl spec', function() {
  var $scope,
      $route,
      $controller,
      $httpBackend;

  // Load the services's module
  beforeEach(module('LocalStorageModule'));
  beforeEach(module('myApp'));

  beforeEach(inject(function(_$controller_, $rootScope, _$route_, _$httpBackend_) {
    $scope = $rootScope.$new();
    $route = _$route_;
    $httpBackend = _$httpBackend_;
    $controller = _$controller_;

    $httpBackend.when('GET', '//localhost:8081/api1/api/things1/').respond({});
    $httpBackend.when('GET', '//localhost:8081/api1/api/things2/').respond({
      'next': 'somepage.html'
    });
    var createController = function(){
      $controller('DashboardCtrl', {$scope: $scope, $route: { current: { params: ''}}});
    }
  }));

  describe('$scope.init()', function() {

    it('Check $scope assignments.', function() {
      createController();
      $httpBackend.flush();
      $scope.$digest();
      expect($scope.displayLoadContainer).toBe(true);
    });
  });
});