Jasmine测试以测试控制器是否已定义 [英] Jasmine test to test if the controller is defined

查看:114
本文介绍了Jasmine测试以测试控制器是否已定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的Jasmine测试角度。我试图测试我定义的控制器是否已定义,但我收到的错误是预期未定义的定义。
这是我的主要内容代码:

New to Jasmine tests for angular. I am trying to test if the controller I defined is defined or not to begin with but I am getting error saying Expected undefined to be defined. Here is my main code:

// controller logic MatchController.js
(function () {
'use strict';

angular.module('app.match')
        .controller('MatchController', MatchController);


MatchController.$inject = ['APP_CONFIG', '$authUser', '$http', '$rootScope', '$state', '$stateParams', 'SearchService', 'ConfirmMatchService', 'MusicOpsService', 'ContentOpsService', 'MatchstickService', 'MatchService', 'Restangular'];
function MatchController(APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, searchService, confirmMatchService, musicOpsService, contentOpsService, matchstickService, matchService, Restangular) {

    var vm = this;
 .
 .
 .
   }
})();

这是测试文件

// MatchController.spec.js
(function(){
'use strict';
describe('Match Controller Tests', function(){

    var module, MatchTestController;

    beforeEach(function() {
        module = angular.module('app.match');
    });

    beforeEach(inject(function ($controller) {

        MatchTestController = $controller('MatchController', {});
    }));

    describe("Match controller to be defined", function() {

        it("should be created successfully", function () {
            expect(MatchTestController).toBeDefined();
        });
    });

});

})();

我一直收到错误:

TypeError: 'undefined' is not a function (evaluating 'angular.controller('MatchController')')
undefined
    at /Users/rgoti/match-ui/match-ui/public/src/app/match/match.controller.spec.js:16
    at invoke (/Users/rgoti/match-ui/match-ui/public/bower_components/angular/angular.js:4219)
    at workFn (/Users/rgoti/match-ui/match-ui/public/bower_components/angular-mocks/angular-mocks.js:2475)
Expected undefined to be defined.
    at /Users/rgoti/match-ui/match-ui/public/src/app/match/match.controller.spec.js:22

不确定我在这里做错了什么。

Not sure what I am doing wrong here.

推荐答案

在模拟之前,你应该首先在控制器中注入所有依赖项。

You should inject all the dependencies in the controller first before mocking it.

试试这个:

// MatchController.spec.js
(function(){
'use strict';
describe('controller: MatchController', function(){

    var module, MatchController, APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, SearchService, ConfirmMatchService, MusicOpsService, ContentOpsService, MatchstickService, MatchService, Restangular;

    beforeEach(function() {
        module = angular.module('app.match');
    });

    beforeEach(inject(function ($controller, _APP_CONFIG_, _$authUser_, _$http_, _$rootScope_, _$state_, _$stateParams_, _SearchService_, _ConfirmMatchService_, _MusicOpsService_, _ContentOpsService_, _MatchstickService_, _MatchService_, _Restangular_) {

        APP_CONFIG = _APP_CONFIG_;
        $authUser = _$authUser_;
        $http = _$http_;
        $rootScope = _$rootScope_;
        $state = _$state_;
        $stateParams = _$stateParams_;
        SearchService = _SearchService_;
        ConfirmMatchService = _ConfirmMatchService_;
        MusicOpsService = _MusicOpsService_;
        ContentOpsService = _ContentOpsService_;
        MatchstickService = _MatchstickService_;
        MatchService = _MatchService_;
        Restangular = _Restangular_;

        MatchController = $controller('MatchController', {
            APP_CONFIG: _APP_CONFIG_,
            $authUser: _$authUser_,
            $http: _$http_,
            $rootScope: _$rootScope_,
            $state: _$state_,
            $stateParams: _$stateParams_,
            SearchService: _SearchService_,
            ConfirmMatchService: _ConfirmMatchService_,
            MusicOpsService: _MusicOpsService_,
            ContentOpsService: _ContentOpsService_,
            MatchstickService: _MatchstickService_,
            MatchService: _MatchService_,
            Restangular: _Restangular_
        });


    }));

    describe("Match controller to be defined", function() {

        it("should be created successfully", function () {
            expect(MatchController).toBeDefined();
        });

    });

});

})();

这篇关于Jasmine测试以测试控制器是否已定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆