2014-10-27 88 views
0

当我运行grunt jasmine我所有规格传球,但是当我运行grunt karma噶因为基本上静静地失败“警告:任务‘因果报应:单位’失败使用--force继续中止由于警告。”告诉我几乎没有任何东西。如果我删除了所有$ httpBackend支票,则grunt karma再次通过。当我添加flush()方法时,似乎发生这种情况。

两个角和角嘲弄都在1.2.26。

有谁知道为什么我的单元测试是在因果报应,而不是在茉莉花失败?

'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_' 
    }); 
}; 
+0

出于好奇:你有什么浏览器设置噶要测试? – Josep 2014-10-27 20:39:02

+0

我认为,我们需要看到您的相关'karma.conf.js'文件。 – Josh 2014-10-27 20:41:29

+0

只是幻影。我能够进入并为记者添加“进步”......我只是在使用“报道”。当我这样做时,噶玛开始给我更详细/有用的输出。我仍然不明白为什么当Karma爆炸时,咕噜咕噜声正在流逝。 – Swordfish0321 2014-10-27 20:41:33

回答

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); 
    }); 
    }); 
}); 
+0

感谢您的回答Josep。我实际上想出了一些非常相似的东西。很高兴能够在你的()语句中调用控制器。然而不幸的是,这并不能解决茉莉花在Karma失败的时候传球的问题。 – Swordfish0321 2014-10-27 21:52:00