使用angularjs中的karma测试控制器时出错
我想使用Karma测试我的应用程序。我已经对它进行了配置,并编写了一个简单的测试套件,用于检查我的应用程序中是否存在控制器。我收到错误“类型错误:无法调用未定义的方法'equal'。我的测试套件条件如下。请建议使用angularjs中的karma测试控制器时出错,angularjs,karma-runner,karma-jasmine,Angularjs,Karma Runner,Karma Jasmine,我想使用Karma测试我的应用程序。我已经对它进行了配置,并编写了一个简单的测试套件,用于检查我的应用程序中是否存在控制器。我收到错误“类型错误:无法调用未定义的方法'equal'。我的测试套件条件如下。请建议 describe('module present', function() { beforeEach(angular.mock.module('demoapp')); it('should have a demoCtrl controller', function() {
describe('module present', function() {
beforeEach(angular.mock.module('demoapp'));
it('should have a demoCtrl controller', function() {
expect(demoapp.ProductCtrl).not.to.equal(null);
});
});
我的karma.config是这样的
files : [
'Scripts/angular.js',
'Scripts/angular-translate.js',
'Scripts/angular-translate-loader-static-files.js',
'Scripts/angular-mocks.js',
'Scripts/angular-*.js',
'Test/lib/angular/angular-mocks.js',
'Scripts/ProjectScript/app.js',
'Scripts/ProjectScript/DemoData.js',
'Scripts/ProjectScript/TimerController.js',
'Scripts/ProjectScript/**/*.js',
'Test/unit/**/*.js'
],
exclude : [
'Scripts/angular-loader.js',
'Scripts/angular-scenario.js'
],
谢谢和问候
utpal试试这个,我希望它能有所帮助
beforeEach(module('demoapp'));
var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('ProductCtrl', {
$scope: scope
});
}));
it('should have a demoCtrl controller', function() {
expect(ctrl).not.to.equal(null);
});
试试这个,我希望有帮助
beforeEach(module('demoapp'));
var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('ProductCtrl', {
$scope: scope
});
}));
it('should have a demoCtrl controller', function() {
expect(ctrl).not.to.equal(null);
});
试试这个,我希望有帮助
beforeEach(module('demoapp'));
var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('ProductCtrl', {
$scope: scope
});
}));
it('should have a demoCtrl controller', function() {
expect(ctrl).not.to.equal(null);
});
试试这个,我希望有帮助
beforeEach(module('demoapp'));
var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('ProductCtrl', {
$scope: scope
});
}));
it('should have a demoCtrl controller', function() {
expect(ctrl).not.to.equal(null);
});
不确定是否显示了整个karma.conf.js文件。无论如何,您应该提到要在配置中使用的测试框架。您需要一个属性
frameworks: ["jasmine"]
下面是一个完整配置的示例。这非常有效
module.exports = function (config) {
config.set({
basepath: '.',
frameworks: ["jasmine"],
//list of file patterns to load in the browser
files: [
'web/public/lib/jquery/jquery-1.9.1.js',
'web/public/lib/angular/angular.min.js',
'web/public/lib/async/async.js',
'test/client/lib/angular/angular-mocks.js',
'web/public/lib/angular-ui/*.js',
'web/public/js/**/*.js',
'test/client/public/js/**/*.js',
'test/client/public/js/**/*.coffee'
],
preprocessors: {
'web/public/js/**/*.js': ['coverage'],
'**/*.coffee': ['coffee']
},
// use dots reporter, as travis terminal does not support escaping sequences
// possible values: 'dots' || 'progress'
reporters: ['progress', 'coverage'],
coverageReporter: {
type: 'lcov',
dir: 'coverage/'
},
// these are default values, just to show available options
// web server port
port: 8089,
// cli runner port
runnerPort: 9109,
//urlRoot = '/__test/';
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// polling interval in ms (ignored on OS that support inotify)
autoWatchInterval: 0,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari
// - PhantomJS
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
不确定是否显示了整个karma.conf.js文件。无论如何,您应该提到要在配置中使用的测试框架。您需要一个属性
frameworks: ["jasmine"]
下面是一个完整配置的示例。这非常有效
module.exports = function (config) {
config.set({
basepath: '.',
frameworks: ["jasmine"],
//list of file patterns to load in the browser
files: [
'web/public/lib/jquery/jquery-1.9.1.js',
'web/public/lib/angular/angular.min.js',
'web/public/lib/async/async.js',
'test/client/lib/angular/angular-mocks.js',
'web/public/lib/angular-ui/*.js',
'web/public/js/**/*.js',
'test/client/public/js/**/*.js',
'test/client/public/js/**/*.coffee'
],
preprocessors: {
'web/public/js/**/*.js': ['coverage'],
'**/*.coffee': ['coffee']
},
// use dots reporter, as travis terminal does not support escaping sequences
// possible values: 'dots' || 'progress'
reporters: ['progress', 'coverage'],
coverageReporter: {
type: 'lcov',
dir: 'coverage/'
},
// these are default values, just to show available options
// web server port
port: 8089,
// cli runner port
runnerPort: 9109,
//urlRoot = '/__test/';
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// polling interval in ms (ignored on OS that support inotify)
autoWatchInterval: 0,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari
// - PhantomJS
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
不确定是否显示了整个karma.conf.js文件。无论如何,您应该提到要在配置中使用的测试框架。您需要一个属性
frameworks: ["jasmine"]
下面是一个完整配置的示例。这非常有效
module.exports = function (config) {
config.set({
basepath: '.',
frameworks: ["jasmine"],
//list of file patterns to load in the browser
files: [
'web/public/lib/jquery/jquery-1.9.1.js',
'web/public/lib/angular/angular.min.js',
'web/public/lib/async/async.js',
'test/client/lib/angular/angular-mocks.js',
'web/public/lib/angular-ui/*.js',
'web/public/js/**/*.js',
'test/client/public/js/**/*.js',
'test/client/public/js/**/*.coffee'
],
preprocessors: {
'web/public/js/**/*.js': ['coverage'],
'**/*.coffee': ['coffee']
},
// use dots reporter, as travis terminal does not support escaping sequences
// possible values: 'dots' || 'progress'
reporters: ['progress', 'coverage'],
coverageReporter: {
type: 'lcov',
dir: 'coverage/'
},
// these are default values, just to show available options
// web server port
port: 8089,
// cli runner port
runnerPort: 9109,
//urlRoot = '/__test/';
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// polling interval in ms (ignored on OS that support inotify)
autoWatchInterval: 0,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari
// - PhantomJS
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
不确定是否显示了整个karma.conf.js文件。无论如何,您应该提到要在配置中使用的测试框架。您需要一个属性
frameworks: ["jasmine"]
下面是一个完整配置的示例。这非常有效
module.exports = function (config) {
config.set({
basepath: '.',
frameworks: ["jasmine"],
//list of file patterns to load in the browser
files: [
'web/public/lib/jquery/jquery-1.9.1.js',
'web/public/lib/angular/angular.min.js',
'web/public/lib/async/async.js',
'test/client/lib/angular/angular-mocks.js',
'web/public/lib/angular-ui/*.js',
'web/public/js/**/*.js',
'test/client/public/js/**/*.js',
'test/client/public/js/**/*.coffee'
],
preprocessors: {
'web/public/js/**/*.js': ['coverage'],
'**/*.coffee': ['coffee']
},
// use dots reporter, as travis terminal does not support escaping sequences
// possible values: 'dots' || 'progress'
reporters: ['progress', 'coverage'],
coverageReporter: {
type: 'lcov',
dir: 'coverage/'
},
// these are default values, just to show available options
// web server port
port: 8089,
// cli runner port
runnerPort: 9109,
//urlRoot = '/__test/';
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// polling interval in ms (ignored on OS that support inotify)
autoWatchInterval: 0,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari
// - PhantomJS
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
让我们看看你的因果报应。js@Edwin上面提到了Dalorzo karma.config文件。不太确定。但是如果您使用的是jasmine,那么它应该是.toBe(null)而不是.to.equal(null)。事实上,我认为正确的方法是
toEqual()
和toBe()
@glepretre和michael是的,你是对的。让我们看看你的karma.conf。js@Edwin上面提到了Dalorzo karma.config文件。不太确定。但是如果您使用的是jasmine,那么它应该是.toBe(null)而不是.to.equal(null)。事实上,我认为正确的方法是toEqual()
和toBe()
@glepretre和michael是的,你是对的。让我们看看你的karma.conf。js@Edwin上面提到了Dalorzo karma.config文件。不太确定。但是如果您使用的是jasmine,那么它应该是.toBe(null)而不是.to.equal(null)。事实上,我认为正确的方法是toEqual()
和toBe()
@glepretre和michael是的,你是对的。让我们看看你的karma.conf。js@Edwin上面提到了Dalorzo karma.config文件。不太确定。但是如果您使用的是jasmine,那么它应该是.toBe(null)而不是.to.equal(null)。事实上,我认为正确的方法是toEqual()
和toBe()
@glepretre和michael是的,你是对的。谢谢你的回答,但它返回的是未定义的而不是null,因为当我编写下面的测试规范时,它给出了未定义的错误。它('should check name',function(){expect(scope.name).toBe(“utpal”);});谢谢您的回答,但它返回的是undefined而不是null,因为当我编写下面的测试规范时,它给出了undefined错误。它('should check name',function(){expect(scope.name).toBe(“utpal”);});谢谢您的回答,但它返回的是undefined而不是null,因为当我编写下面的测试规范时,它给出了undefined错误。它('should check name',function(){expect(scope.name).toBe(“utpal”);});谢谢您的回答,但它返回的是undefined而不是null,因为当我编写下面的测试规范时,它给出了undefined错误。它('should check name',function(){expect(scope.name).toBe(“utpal”);};