我有一个用angular.module().controller()创建的控制器,就像下面这种情况
myModule = angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope', function($scope){
$scope.test = 'this is a test';
}]);
现在,我需要使用mocha来测试我的控制器是否工作正常。在Angular中,有一些将控制器声明为全局函数的示例(例如,http://docs.angularjs.org/tutorial/step_04 ),所以他们使用
function PhoneListCtrl() {...}
.....
beforeEach(function() {
scope = {},
ctrl = new PhoneListCtrl(scope);
});
it('shod test whatever PhoneListCtrl does ', function() {
expect(scope.someProp).toBe('whateverValue');
});
所以问题是:
1)如何对使用angular.module().controller()声明的控制器执行类似的测试
2)如何使用Mocha
发布于 2012-12-11 03:33:49
如果您使用的是mocha,请注意,除非您已升级到angular-1.1.1,否则不支持angular.mock.module
或angular.mock.inject
。我也是一样的,但是因为另一个问题我不能升级。
我想使用mocha,因为我的服务器端测试是在mocha中进行的,而我更喜欢在两端使用相同的测试框架,所以我有点为难。
所以,如果你不能使用inject/module,那么你可以这样尝试:
var $injector = angular.injector(['your-app-name', 'ng']),
$controller = $injector.get('$controller'),
$scope = $injector.get('$rootScope');
describe('my app controllers', function () {
describe('FooCtrl', function () {
it('should do something', function () {
// scope can be any object you want; could be $rootScope from above
var params = { $scope: { } },
ctrl = $controller('FooCtrl', params);
// TODO: test ctrl
});
});
});
https://stackoverflow.com/questions/13585964
复制相似问题