我想在AngularJS app中测试控制器

时间:2015-01-20 11:03:09

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

我无法解决依赖关系。

例如,在测试此控制器时:

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]无法实例化模块undefined   由于:错误:[ng:areq]参数' fn'得到了,不是一个功能   未定义     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         在assertArg(/home/nazar/cardgame/app/bower_components/angular/angular.js:1577:11)         在assertArgFn(/home/nazar/cardgame/app/bower_components/angular/angular.js:1587:3)         atnotate(/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)         at Object.workFn(/home/nazar/cardgame/app/bower_components/angular/angular-mocks.js:2172:52)     http://errors.angularjs.org/1.3.9/ $注射器/ modulerr P0 =未定义&安培; P1 =错误%3A%20%5Bng%3Aareq%5D%20Argument%20' FN'%图20是%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%20原子%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A63 3A12%%0A%20%20%20%20原子%20assertArg%20(HTTP%3A%2F%2Flocalhost%3A9876 %2Fbase%2Fbower_components%2Fangular%2Fangular.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%20原子%20annotate%20(HTTP%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F2e97ef8392d92d35b3be31ddd02a442485fb2dcd%3A3468%3A5)% 0A%20%20%20%20原子%20Object.invoke%20(HTTP%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_c omponents%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%20原子%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%20原子%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%20原子%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         在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)         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):执行1 of 1(1失败)错误(0.039秒)   / 0.031秒)

**我认为问题如下:

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

}));

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

1 个答案:

答案 0 :(得分:0)

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

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