0

我已經開發了一個基於AngularJS和基於引導的額外工具的應用程序。 現在我想測試一切。 但我有一個噶瑪和茉莉花的問題。 問題是業力創建例外,當我啓動測試...JavaScript的單元測試:在Karma,茉莉花和引導程序上進行測試的依賴關係

的例外是TypError,對象不是一個方法(很抱歉,這是在法國我的控制檯上)

這裏是我的karma.conf.js:

module.exports = function(config) { 
config.set({ 

basePath: '', 

frameworks: ['jasmine'], 

files: [ 
    'src/test/lib/angular/angular.js', 
    'src/test/lib/angular/angular-mocks.js', 
    'src/test/lib/angular/angular-route.min.js', 
    'src/test/lib/jquery-1.9.1.min.js', 
    'src/main/webapp/resources/js/pages/*.js', 
    'src/main/webapp/resources/js/pages/survey/*.js', 
    'src/main/webapp/resources/js/pages/billing/*.js', 
    'src/test/*Spec.js', 
    'src/test/survey/*Spec.js', 
    'src/test/billing/*Spec.js', 
], 
exclude: [ 
], 

preprocessors: { 
}, 

reporters: ['progress'], 

port: 9876, 

colors: true, 

logLevel: config.LOG_INFO, 

autoWatch: true, 

browsers: ['IE', 'Firefox'], 

junitReporter: { 
     outputFile: 'unit.xml', 
     suite: 'unit' 
} 
}); 
}; 
在我的JSP和JS

,我用一個自舉選庫由西爾維奧moreto:http://silviomoreto.github.io/bootstrap-select/

,這是創建異常的代碼:

angular.module("searchSurveyApp", []) 
.controller("searchSurveyController" , function($scope, $http,$window) { 


    ... 

    $("#selectpicker").selectpicker(); // <= launches exception in karma 

,最後,這是JS測試:

describe('Unit: searchSurveyController', function() { 
    // Load the module with MainController 
    beforeEach(module('searchSurveyApp')); 

    var ctrl, scope, http; 
    // inject the $controller and $rootScope services 
    // in the beforeEach block 
    beforeEach(inject(function($controller, $rootScope, $http) { 
    // Create a new scope that's a child of the $rootScope 
    scope = $rootScope.$new(); 
    http = $http; 
    // Create the controller 
    ctrl = $controller('searchSurveyController', { 
     $scope: scope, 
     $http: http 
    }); 
    })); 


    it('should write hello', 
    function() { 
     console.log("hello"); 
    }); 

}); 

如果我刪除方法:$( 「#selectpicker」)selectpicker(); 測試可以。所以,我問你,我該如何測試這種類型的代碼?

感謝由提前,因爲我真的卡在這個

回答

0

你不能只使用DOM在單元測試中,因爲這是單元測試,而不是最終-2-結束。茉莉花環境中沒有真正的DOM可用。像「$(」#selectpicker「)。selectpicker();」應該內部指令來incapsulated,並且可以單獨使用其他方法與虛擬DOM(https://egghead.io/lessons/angularjs-unit-testing-a-directive

在你的服務或工廠,你要測試的所有DOM操作是不可測的測試指令,所以在適當的方式提取任何重構你的服務DOM工作指令

+0

你是對的!我把我所有的選擇器變成了一個很酷的指令,現在,它可以工作,非常感謝! – user3753210 2014-08-27 12:56:44