angularjs - Jasmine 测试来测试 Controller 是否已定义

标签 angularjs jasmine karma-jasmine

Jasmine Angular 测试的新功能。我正在尝试测试我定义的 Controller 是否已定义,但我收到错误消息 Expected undefined to be Defined. 这是我的主要代码:

// 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

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

最佳答案

在模拟 Controller 之前,您应该先将所有依赖项注入(inject) Controller 中。

试试这个:

// 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();
        });

    });

});

})();

关于angularjs - Jasmine 测试来测试 Controller 是否已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37714084/

相关文章:

angular - 类型错误 : Cannot read property 'subscribe' of undefined in unit test

javascript - AngularJS 高级缓存技术

javascript - 如何使用 angularjs 将数据库中的视频链接嵌入 iframe 中

javascript - 有人有 JXBrowser 的 Karma 启动器吗?

javascript - 使用 angular-translate 调用对 AngularJS 服务进行单元测试

javascript - 无法将工厂注入(inject) Jasmine 中的 Controller (requireJS)

jenkins - 部署到生产环境以进行角度测试覆盖时如何在 jenkins 中设置 CHROME_BIN

javascript - ng-options 中的键值对

AngularJS - http 拦截器 - 在 token 刷新后重新发送所有请求

javascript - 如何使用 Angular 对带有 Promise 和回调函数的函数进行单元测试