简体   繁体   English

在茉莉花中注入角度控制器

[英]Injecting angular controller in jasmine

I have read through many examples in regards to injecting controllers into a jasmine unit test, however I keep getting "Error: [ng:areq] http://errors.angularjs.org/undefined/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined ". 我已经阅读了许多有关将控制器注入茉莉花单元测试的示例,但是我不断收到“错误:[ng:areq] http://errors.angularjs.org/undefined/ng/areq?p0=MainCtrl&p1=not %20a%20function%2C%20got%20undefined “。

Here is my code: 这是我的代码:

main.spec.js: main.spec.js:

'use strict'

describe("Testing Main Controller", function(){
    var scope, controller;
    var dummyFunction = function(){};
    var defaultDocument = {
        _id: "123456"
    };
    beforeEach(module('app.controllers'));
    beforeEach(module('app'));
    beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        controller = $controller('MainCtrl', {
            $scope: scope,
            SearchService: dummyFunction,
            ResultsService: dummyFunction,
            FacetService: dummyFunction,
            EsDateService: dummyFunction,
            Likes: dummyFunction,
            Bookmarks: dummyFunction
        });
    }));
    describe("Likes", function(){
        it('shall give the user the ability to like a document that is currently being displayed.', function(){
            scope.updateLike([defaultDocument]);
            expect(defaultDocument.isLiked).toBe(true);
        });
        it('shall give the user the ability to remove a like from a document that is currently being displayed.', function(){
            defaultDocument.isLiked = true;
            scope.updateLike([defaultDocument]);
            expect(defaultDocument.isLiked).toBe(true);
        });
    });
});

main_controller.js: main_controller.js:

'use strict';

angular.module('app.controllers')
    .controller('MainCtrl', function($scope, SearchService, ResultsService, FacetService, EsDateService, Likes, Bookmarks) {

});

app.js: app.js:

angular.module('app.services', ['ngResource', 'elasticjs.service']);
angular.module('app.controllers', [ 'app.services']);

var app = angular.module('app', [
    'ui.bootstrap',
    'elasticjs.service',
    'app.services',
    'app.controllers',
    'app.config',
    'facet.directives',
    'ngRoute']);
    app.config(['$routeProvider',
        function($routeProvider) {
            $routeProvider
                .when('/', {
                    controller: 'SearchCtrl',
                    templateUrl: 'views/search/search.html'
                })
                .when('/journal', {
                    controller: 'JournalCtrl',
                    templateUrl: 'views/journal/journal.html'
                })
                .otherwise({
                    redirectTo: '/'
                });
        }
    ]);
    app.config(['$locationProvider',
        function($locationProvider) {
            $locationProvider.hashPrefix("!");
        }
    ]);

When I attach MainCtrl to app rather than app.controllers it seems to find MainCtrl. 当我将MainCtrl附加到app而不是app.controllers时,似乎找到了MainCtrl。 What am I doing wrong? 我究竟做错了什么?

You don't need to re-declare dependencies for app module, as app module injects app.controllers 您无需为应用模块重新声明依赖项,因为应用模块会注入app.controllers

beforeEach(module('app'));

Quick example how it can be solved - http://jsfiddle.net/PtXFb/ 快速示例如何解决-http://jsfiddle.net/PtXFb/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM