2014-10-27 95 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