我想测试一个控制器,但我得到一个错误
I'm trying to test a controller but I'm getting an error
类型错误:对象#没有方法'申请'的ReferenceError: 注入没有定义
TypeError: Object # has no method 'apply' ReferenceError: inject is not defined
该单位test.js是
The unit-test.js is
define(['angular',
'myApp',
'angularMocks',
'MyCtrl'
], function() {
describe('Testing controller', function() {
var $scope = null;
var ctrl = null;
beforeEach(angular.module('myApp'));
beforeEach(inject(function ($injector) {
$scope = $injector.get('$rootScope');
$controller = $injector.get('$controller');
}));
describe('MyCtrl', function () {
it('should call test variable', function () {
$scope = $rootScope.$new();
ctrl = $controller('MyCtrl', {
$scope: $scope
});
expect($scope.test).toBe("sth");
});
});
});
});
在MyCtrl我已经声明了一个 $ scope.test =某物;
in MyCtrl I have declared a $scope.test = "sth";
在更改
beforeEach(angular.module('对myApp'));
到 beforeEach(模块('对myApp'));
我收到的ReferenceError:模块没有定义
我用噶版本:0.9.8和AngularJS v1.0.8
I use Karma version: 0.9.8 and AngularJS v1.0.8
非常感谢你!
您有很多事情,如果你使用requirejs做。
You have a lot of things to do if you're using requirejs.
首先,你必须把卡玛requirejs
插件,在的package.json
First you have to put the karma-requirejs
plugin in your package.json
"karma-requirejs": "~0.1.0",
然后,你必须TI更改配置文件。你必须添加 requirejs
在框架
部分然后,排除您的要求 main.js
然后通过模式
配置,而不是包含
是
Then add all your librairies files via the pattern
config and not include
it
您必须添加您需要主test.js
(配置文件测试描述在底部)
You have to add your require main-test.js
(config file for test describe at bottom)
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', 'requirejs'],
files: [
{pattern: 'app/bower_components/jquery/jquery.min.js', included: false},
{pattern: 'app/bower_components/angular/angular.min.js', included: false},
{pattern: 'app/bower_components/angular-resource/angular-resource.min.js', included: false},
{pattern: 'app/bower_components/angular-mocks/angular-mocks.js', included: false},
{pattern: 'app/scripts/*.js', included: false},
{pattern: 'app/scripts/**/*.js', included: false},
{pattern: 'test/spec/**/*Spec.js', included: false},
'test/main-test.js'
],
exclude: ['app/scripts/main.js'],
port: 8082,
logLevel: config.LOG_DEBUG,
autoWatch: false,
browsers: ['Chrome'],
singleRun: false
});
};
现在创建主test.js
。
在你必须让所有的测试文件,把它作为依赖关系。
In you have to get all your tests files to put it as dependencies.
然后做一个经典的requirejs配置(注意在的baseUrl
我们使用 /基
噶常数),最后由code通过启动因缘:窗口.__人缘__开始();
Then do a classic requirejs config (note in the baseUrl
we use /base
a karma constant) and finally start karma by code through : window.__karma__.start();
例如:
var tests = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/Spec\.js$/.test(file)) {
tests.push(file);
}
}
}
require.config({
// Karma serves files from '/base'
baseUrl: '/base/app/scripts',
paths: {
jquery: '../bower_components/jquery/jquery.min',
angular: '../bower_components/angular/angular.min',
angularMocks: '../bower_components/angular-mocks/angular-mocks',
ngResource: '../bower_components/angular-resource/angular-resource.min'
},
shim: {
jquery: {
exports: '$'
},
angular: {
deps: [ 'jquery', 'bootstrap'],
exports: 'angular'
},
ngResource: {
deps: [ 'angular' ],
exports: 'ngResource'
},
angularMocks: {
deps: [ 'ngResource' ],
exports: 'angularMocks'
}
},
priority: [
'angular'
],
// ask Require.js to load these files (all our tests)
deps: tests
});
require(tests, function(){
window.__karma__.start();
});
在您的测试文件:
变更 beforeEach(angular.module('对myApp'));
到 beforeEach(模块('对myApp'));
更改您的PARAMS定义这样的:
Change the params of your define like that :
define(['angular',
'myApp',
'angularMocks',
'MyCtrl'
], function(angular, myApp, angularMocks, MyCtrl)
要注入控制器只是做了(你必须把 MyCtrl
在你需要的功能参数):
To inject controller just do that (You have to put the MyCtrl
in param of your require function) :
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
$controller(MyCtrl, {
$scope: scope
});
}));
最后:
it('should call crudListMethods', function () {
expect(scope.test).toBe("sth");
});
现在它应该工作!希望它能帮助!
Now it should work ! Hope it helps !