Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript的单元测试:Karma、jasmine和bootstrap的测试依赖项_Javascript_Angularjs_Twitter Bootstrap_Jasmine_Karma Runner - Fatal编程技术网

JavaScript的单元测试:Karma、jasmine和bootstrap的测试依赖项

JavaScript的单元测试:Karma、jasmine和bootstrap的测试依赖项,javascript,angularjs,twitter-bootstrap,jasmine,karma-runner,Javascript,Angularjs,Twitter Bootstrap,Jasmine,Karma Runner,我已经用AngularJS和一些基于引导的额外工具开发了一个应用程序。 现在我想测试一切。 但是我对因果报应和茉莉花有个问题。 问题是,当我启动测试时,因果报应会产生一个异常 例外是TypeError,对象不是方法(对不起,在我的控制台上是法语) 这是我的karma.conf.js: module.exports = function(config) { config.set({ basePath: '', frameworks: ['jasmine'], files: [ 'src/

我已经用AngularJS和一些基于引导的额外工具开发了一个应用程序。 现在我想测试一切。 但是我对因果报应和茉莉花有个问题。 问题是,当我启动测试时,因果报应会产生一个异常

例外是TypeError,对象不是方法(对不起,在我的控制台上是法语)

这是我的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中,我使用silvio moreto的引导选择库:

这是创建异常的代码:

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


    ...

    $("#selectpicker").selectpicker(); // <= launches exception in karma
如果我删除此项:$(“#selectpicker”).selectpicker(); 测试还可以。 那么,我问你,我如何测试这种代码


提前感谢,因为我真的坚持了这个

你不能只在单元测试中使用DOM,因为那是单元测试,而不是end-2-end。jasmine环境中没有真正的DOM可用。诸如“$(“#selectpicker”).selectpicker();”之类的内容应该包含在指令中,您可以使用虚拟DOM()的其他方法单独测试指令


在您的服务或工厂中,您希望测试任何DOM操作都是不稳定的,因此请以适当的方式重构您的服务,以将任何DOM工作提取到指令中

您是对的!我把我所有的选择选择器转换成一个很酷的指令,现在,它工作了,非常感谢!
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");
  });

});