我想在 AngularJS 应用程序中测试控制器
I want to test controllers in AngularJS app
我无法解决依赖关系。
例如测试这个控制器时:
var MyApp = angular.module("MyApp");
MyApp.controller("GameController", ["$scope", "$interval" , "MY_CONST" , "Auth" , "CustomActionService" , "GamesFactory", "GameFactory", "$location", function($scope, $interval , MY_CONST , Auth , CustomActionService , GamesFactory, GameFactory, $location){
$scope.testVar = "test text";
$scope.indexGames = function(){
GamesFactory.query({}, function(data) {
$scope.games = data;
}, function(error) {
console.log(error);
}
);
}
$scope.deleteGame = function(game){
if(confirm("Are you sure?")){
GameFactory.delete({id: game.id},
function(){
$scope.indexGames();
});
}
};
$scope.createGame = function(){
GamesFactory.create({game: $scope.newGame}, function(data, $scope){
$location.path(MY_CONST.GAMES_PATH + data.id);
}, function(error){
console.log(error);
});
};
$scope.joinGame = function(game){
CustomActionService.join({id: game.id , action: MY_CONST.ACTION_UPDATE}, function(){
$location.path(MY_CONST.GAMES_PATH + game.id);
});
};
$scope.showGame = function(game) {
$location.path(MY_CONST.GAMES_PATH + game.id);
};
$scope.resolveUser();
$scope.indexGames();
$interval(function(){
setTimeout(function() {
$scope.$apply(function() {
$scope.indexGames();
});
}, MY_CONST.TIMEOUT);
}, MY_CONST.INTERVAL);
}]);
我运行一个测试:
'use strict';
/* jasmine specs for controllers go here */
describe('MyApp controllers', function() {
var TEST_GAMES = {
"id": 1,
"state": "expactation_second_player",
"winner": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"loser": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"attacker": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"defender": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"mover": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"name": "qwe",
"description": "qweqwe",
"created_at": "2015-01-19T16:02:12.133Z",
"updated_at": "2015-01-19T16:02:12.783Z"
};
beforeEach(function() {
angular.mock.module('ngResource', []);
angular.mock.module('ngRoute', []);
angular.mock.module('Devise', []);
module('MyApp', ['ngResource', 'ngRoute', 'Devise']);
});
describe('GameCtrl', function() {
var scope, $httpBackend, ctrl;
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
}));
it('should to create "testVar" variable with value "test text"', function() {
});
});
});
测试失败并出现错误:
Error: [$injector:modulerr] Failed to instantiate module undefined
due to: Error: [ng:areq] Argument 'fn' is not a function, got
undefined
http://errors.angularjs.org/1.3.9/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20undefined
at /home/nazar/cardgame/app/bower_components/angular/angular.js:63:12
at assertArg (/home/nazar/cardgame/app/bower_components/angular/angular.js:1577:11)
at assertArgFn (/home/nazar/cardgame/app/bower_components/angular/angular.js:1587:3)
at annotate (/home/nazar/cardgame/app/bower_components/angular/angular.js:3468:5)
at Object.invoke (/home/nazar/cardgame/app/bower_components/angular/angular.js:4160:21)
at /home/nazar/cardgame/app/bower_components/angular/angular.js:4101:45
at forEach (/home/nazar/cardgame/app/bower_components/angular/angular.js:323:20)
at loadModules (/home/nazar/cardgame/app/bower_components/angular/angular.js:4078:5)
at Object.createInjector [as injector] (/home/nazar/cardgame/app/bower_components/angular/angular.js:4004:11)
at Object.workFn (/home/nazar/cardgame/app/bower_components/angular/angular-mocks.js:2172:52)
http://errors.angularjs.org/1.3.9/$injector/modulerr?p0=undefined&p1=Error%3A%20%5Bng%3Aareq%5D%20Argument%20'fn'%20is%20not%20a%20function%2C%20got%20undefined%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.3.9%2Fng%2Fareq%3Fp0%3Dfn%26p1%3Dnot%2520a%2520function%252C%2520got%2520undefined%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A63%3A12%0A%20%20%20%20at%20assertArg%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A1577%3A11)%0A%20%20%20%20at%20assertArgFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A1587%3A3)%0A%20%20%20%20at%20annotate%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A3468%3A5)%0A%20%20%20%20at%20Object.invoke%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4160%3A21)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4101%3A45%0A%20%20%20%20at%20forEach%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A323%3A20)%0A%20%20%20%20at%20loadModules%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4078%3A5)%0A%20%20%20%20at%20Object.createInjector%20%5Bas%20injector%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4004%3A11)%0A%20%20%20%20at%20Object.workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular-mocks.js%3F341e88b947784ad0944e05fb968378088b46efba%3A2172%3A52)
at /home/nazar/cardgame/app/bower_components/angular/angular.js:63:12
at forEach (/home/nazar/cardgame/app/bower_components/angular/angular.js:323:20)
at loadModules (/home/nazar/cardgame/app/bower_components/angular/angular.js:4078:5)
at Object.createInjector [as injector] (/home/nazar/cardgame/app/bower_components/angular/angular.js:4004:11)
at Object.workFn (/home/nazar/cardgame/app/bower_components/angular/angular-mocks.js:2172:52)
at /home/nazar/cardgame/app/bower_components/angular/angular.js:4117:15
Chrome 39.0.2171 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.039 secs
/ 0.031 secs)
**我认为问题如下:
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
}));
https://github.com/SoftServeUniversity/cardgame/tree/AngularMaster
我想,你忘记在模块中添加依赖项了。试试这个
var MyApp = angular.module("MyApp",[]);
我无法解决依赖关系。
例如测试这个控制器时:
var MyApp = angular.module("MyApp");
MyApp.controller("GameController", ["$scope", "$interval" , "MY_CONST" , "Auth" , "CustomActionService" , "GamesFactory", "GameFactory", "$location", function($scope, $interval , MY_CONST , Auth , CustomActionService , GamesFactory, GameFactory, $location){
$scope.testVar = "test text";
$scope.indexGames = function(){
GamesFactory.query({}, function(data) {
$scope.games = data;
}, function(error) {
console.log(error);
}
);
}
$scope.deleteGame = function(game){
if(confirm("Are you sure?")){
GameFactory.delete({id: game.id},
function(){
$scope.indexGames();
});
}
};
$scope.createGame = function(){
GamesFactory.create({game: $scope.newGame}, function(data, $scope){
$location.path(MY_CONST.GAMES_PATH + data.id);
}, function(error){
console.log(error);
});
};
$scope.joinGame = function(game){
CustomActionService.join({id: game.id , action: MY_CONST.ACTION_UPDATE}, function(){
$location.path(MY_CONST.GAMES_PATH + game.id);
});
};
$scope.showGame = function(game) {
$location.path(MY_CONST.GAMES_PATH + game.id);
};
$scope.resolveUser();
$scope.indexGames();
$interval(function(){
setTimeout(function() {
$scope.$apply(function() {
$scope.indexGames();
});
}, MY_CONST.TIMEOUT);
}, MY_CONST.INTERVAL);
}]);
我运行一个测试:
'use strict';
/* jasmine specs for controllers go here */
describe('MyApp controllers', function() {
var TEST_GAMES = {
"id": 1,
"state": "expactation_second_player",
"winner": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"loser": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"attacker": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"defender": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"mover": {
"id": null,
"user_id": null,
"game_id": null,
"player_cards": [],
"cards_count": null,
"created_at": null,
"updated_at": null
},
"name": "qwe",
"description": "qweqwe",
"created_at": "2015-01-19T16:02:12.133Z",
"updated_at": "2015-01-19T16:02:12.783Z"
};
beforeEach(function() {
angular.mock.module('ngResource', []);
angular.mock.module('ngRoute', []);
angular.mock.module('Devise', []);
module('MyApp', ['ngResource', 'ngRoute', 'Devise']);
});
describe('GameCtrl', function() {
var scope, $httpBackend, ctrl;
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
}));
it('should to create "testVar" variable with value "test text"', function() {
});
});
});
测试失败并出现错误:
Error: [$injector:modulerr] Failed to instantiate module undefined due to: Error: [ng:areq] Argument 'fn' is not a function, got undefined http://errors.angularjs.org/1.3.9/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20undefined at /home/nazar/cardgame/app/bower_components/angular/angular.js:63:12 at assertArg (/home/nazar/cardgame/app/bower_components/angular/angular.js:1577:11) at assertArgFn (/home/nazar/cardgame/app/bower_components/angular/angular.js:1587:3) at annotate (/home/nazar/cardgame/app/bower_components/angular/angular.js:3468:5) at Object.invoke (/home/nazar/cardgame/app/bower_components/angular/angular.js:4160:21) at /home/nazar/cardgame/app/bower_components/angular/angular.js:4101:45 at forEach (/home/nazar/cardgame/app/bower_components/angular/angular.js:323:20) at loadModules (/home/nazar/cardgame/app/bower_components/angular/angular.js:4078:5) at Object.createInjector [as injector] (/home/nazar/cardgame/app/bower_components/angular/angular.js:4004:11) at Object.workFn (/home/nazar/cardgame/app/bower_components/angular/angular-mocks.js:2172:52) http://errors.angularjs.org/1.3.9/$injector/modulerr?p0=undefined&p1=Error%3A%20%5Bng%3Aareq%5D%20Argument%20'fn'%20is%20not%20a%20function%2C%20got%20undefined%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.3.9%2Fng%2Fareq%3Fp0%3Dfn%26p1%3Dnot%2520a%2520function%252C%2520got%2520undefined%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A63%3A12%0A%20%20%20%20at%20assertArg%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A1577%3A11)%0A%20%20%20%20at%20assertArgFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A1587%3A3)%0A%20%20%20%20at%20annotate%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A3468%3A5)%0A%20%20%20%20at%20Object.invoke%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4160%3A21)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4101%3A45%0A%20%20%20%20at%20forEach%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A323%3A20)%0A%20%20%20%20at%20loadModules%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4078%3A5)%0A%20%20%20%20at%20Object.createInjector%20%5Bas%20injector%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4004%3A11)%0A%20%20%20%20at%20Object.workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular-mocks.js%3F341e88b947784ad0944e05fb968378088b46efba%3A2172%3A52) at /home/nazar/cardgame/app/bower_components/angular/angular.js:63:12 at forEach (/home/nazar/cardgame/app/bower_components/angular/angular.js:323:20) at loadModules (/home/nazar/cardgame/app/bower_components/angular/angular.js:4078:5) at Object.createInjector [as injector] (/home/nazar/cardgame/app/bower_components/angular/angular.js:4004:11) at Object.workFn (/home/nazar/cardgame/app/bower_components/angular/angular-mocks.js:2172:52) at /home/nazar/cardgame/app/bower_components/angular/angular.js:4117:15 Chrome 39.0.2171 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.039 secs / 0.031 secs)
**我认为问题如下:
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
}));
https://github.com/SoftServeUniversity/cardgame/tree/AngularMaster
我想,你忘记在模块中添加依赖项了。试试这个
var MyApp = angular.module("MyApp",[]);