【问题标题】:I want to test controllers in AngularJS app我想在 AngularJS 应用程序中测试控制器
【发布时间】:2015-03-18 14:02:29
【问题描述】:

我无法解决依赖关系。

例如,在测试这个控制器时:

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() {

        });
    });
});

测试失败并出现错误:

错误:[$injector:modulerr] 无法实例化未定义的模块 由于:错误:[ng:areq] 参数 'fn' 不是函数,得到 不明确的 http://errors.angularjs.org/1.3.9/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20undefined 在/home/nazar/cardgame/app/bower_components/angular/angular.js:63:12 在 assertArg (/home/nazar/cardgame/app/bower_components/angular/angular.js:1577:11) 在 assertArgFn (/home/nazar/cardgame/app/bower_components/angular/angular.js:1587:3) 在注释(/home/nazar/cardgame/app/bower_components/angular/angular.js:3468:5) 在 Object.invoke (/home/nazar/cardgame/app/bower_components/angular/angular.js:4160:21) 在/home/nazar/cardgame/app/bower_components/angular/angular.js:4101:45 在 forEach (/home/nazar/cardgame/app/bower_components/angular/angular.js:323:20) 在 loadModules (/home/nazar/cardgame/app/bower_components/angular/angular.js:4078:5) 在 Object.createInjector [作为注入器] (/home/nazar/cardgame/app/bower_components/angular/angular.js:4004:11) 在 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%2F2Flocalhost%2angular_Fbower组件%2Fangular%2的.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A1577%3A11)%0A%20%20%20%20原子%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%3A20%%0A%20%20 20at%20Object.invoke%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_compone NTS%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4160%3A21)%0A%20%20%20%20原子%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 20at%20loadModules%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4078%3A5)%0A%20%20%20%20at%5BaorsObject. %20injector%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A4004%3A11)%0A%20%20%20%20at%20Object. http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular-mocks.js%3F341e88b947784ad0944e05fb968378088b46efba%3A2172%3A52) 在/home/nazar/cardgame/app/bower_components/angular/angular.js:63:12 在 forEach (/home/nazar/cardgame/app/bower_components/angular/angular.js:323:20) 在 loadModules (/home/nazar/cardgame/app/bower_components/angular/angular.js:4078:5) 在 Object.createInjector [作为注入器] (/home/nazar/cardgame/app/bower_components/angular/angular.js:4004:11) 在 Object.workFn (/home/nazar/cardgame/app/bower_components/angular/angular-mocks.js:2172:52) 在/home/nazar/cardgame/app/bower_components/angular/angular.js:4117:15 Chrome 39.0.2171 (Linux):执行 1 of 1 (1 FAILED) 错误 (0.039 秒 / 0.031 秒)

**我认为问题如下:

beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {

}));

https://github.com/SoftServeUniversity/cardgame/tree/AngularMaster

【问题讨论】:

    标签: angularjs unit-testing karma-runner karma-jasmine


    【解决方案1】:

    我想,你忘了在模块中添加依赖项。试试这个

    var MyApp = angular.module("MyApp",[]); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      相关资源
      最近更新 更多