我很想通过angular-mock和jasmine编写一个简单的angularjs控制器测试用例。
我正在使用angularjs v1.0.8和jasmine 1.3.0。
我的代码是这样的:
1。 demoController.js
'use strict';
var app = angular.module('MyApp', []);
app.controller('MainCtrl', function($scope) {
$scope.data = 'hello';
});
2。 controllerTest.js
'use strict';
describe('MainCtrl', function () {
var scope = null;
beforeEach(angular.module('MyApp'));
beforeEach(inject(function($rootScope, $controller){
scope = $rootScope.$new();
$controller('MainCtrl', {$scope: scope});
}));
it('should have variable text = "Hello World!"', function(){
expect(scope.data).toBe('hello');
});
});
从我的SpecRunner.htm文件中可以找到路径中的以下文件。
angular-1.0.8.min.js
角mocks.js
角scenario.js
角resource.js
jasmine.js
茉莉html.js
demoControllers.js
controllerTest.js“
现在我执行html文件,收到以下错误。
**TypeError: Object #<Object> has no method 'apply'**
**ReferenceError: inject is not defined**
at null.<anonymous>
所以我的问题是,我做错了什么或错过任何配置?
答案 0 :(得分:0)
试试此代码
beforeEach(module('MyApp'));
describe('MainCtrl', function () {
var scope = null;
beforeEach(inject(function($rootScope, $controller){
scope = $rootScope.$new();
$controller('MainCtrl', {$scope: scope});
}));
it('should have variable text = "Hello World!"', function(){
expect(scope.data).toBe('hello');
});
});