我必须为 Controller 编写 karma-jasmine 测试用例
(->
class AddUserController
@$inject = ['$scope', '$http', '$state', 'UserFactory']
constructor: (@$scope, @$http, @$state, UserFactory) ->
@user = new UserFactory()
angular
.module('app', [])
.controller('AddUserController', AddUserController)
)()
但是当我在测试用例中注入(inject) AddUserController 时,它给了我未知的提供者:
describe('add_user_controller', function() {
var addUserController, $httpBackend;
beforeEach(module("app"));
beforeEach(
inject( function($injector, $rootScope) {
addUserController = $injector.get('AddUserController')
})
);
it('should have initialize values', function() {
expect(addUserController.user).toBeDefined();
})
});
任何人都可以猜猜这里出了什么问题吗?
这是 karma.conf.js 代码
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'*.coffee',
'test/*.coffee'
],
preprocessors: {
'*.coffee': ['coffee']
},
plugin: [
'karma-coffee-preprocessor',
'karma-jasmine',
'karma-chrome-launcher',
],
autoWatch: true,
browsers: ['Chrome']
});
};
我的 addUserController.coffee 和 karma.conf.js 位于同一根(级别)。
最佳答案
您应该通过将 Controller 名称传递给 $controller
服务来获取 Controller 实例。就像下面这样
scope = $rootScope.$new(true);
//inject `$controller` before use it.
addUserController = $controller('AddUserController', { $scope: scope });
关于angularjs - 带有 es6 模块的 Angular 测试用例中的未知提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38803666/