当使用参数实例化时,AngularJS Jasmine错误'Controller不是函数'

时间:2014-12-06 07:42:20

标签: angularjs jasmine angular-mock

我一直在做angularJS一段时间(没有测试),但我想做得好!我有一个像这样定义的控制器

(function () {
'use strict';

angular.module('app')
    .controller('CarehomeListCtrl', ['$scope', 'carehomesDataService', carehomeListCtrl]);


function carehomeListCtrl($scope, carehomesDataService) {
    var vm = this;
    vm.carehomeCollection = [];

    vm.activate = activate;


    function activate() {
        vm.carehomeCollection = carehomesDataService.getAllCarehomes();
    }

    activate();
}
})();

然后我的规格

   describe("Carehomes tests", function () {
    var $scopeConstructor, $controllerConstructor;

    beforeEach(module('app'));

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

        $controllerConstructor = $controller;
        $scopeConstructor = $rootScope;
    }));

    describe("CarehomeListCtrl", function () {
        var ctrl, dataService, scope;
        function createController() {
            return $controllerConstructor('CarehomeListCtrl', {
                $scope: scope,
                carehomesDataService: dataService
            });

        }
        beforeEach(inject(function ($injector) {
            scope = $scopeConstructor.$new();
            dataService =$injector.get('carehomesDataService') ;
        }));

        it("should have a carehomesCollection array", function () {
            ctrl = createController();
            expect(ctrl.carehomesCollection).not.toBeNull();
        });

        it("should have 3 items in carehomesCollection array when load is called", function () {
            ctrl = createController();
            expect(ctrl.carehomeCollection.length).toBe(3);
        });
    });


});

这里的问题是,实例化我的控制器的调用失败并且每当我用任何参数调用它时都会出错,无论是空对象{}还是只是$ scope:scope}所以我知道问题不是carehomesDataService。

  

结果StackTrace:错误:[ng:areq]参数' CarehomeListCtrl'不是   一个函数,未定义   http://errors.angularjs.org/1.2.26/ng/areq?p0=CarehomeListCtrl&p1=not%20a%20function%2C%20got%20undefined

但是,如果我在没有参数的情况下实例化这个$controllerConstructor('CarehomeListCtrl');的控制器,它就会被实例化。我很难过!

carehomesDataService是我编写的一个自定义服务,但是它自己的测试通过,并且它被正确地注入到应用程序的控制器中。

任何帮助都会受到大力赞赏。

注意:我不完全同意将控制器上的属性定义为视图模型而不是$ scope,但我遵循Jesse Liberty的复数课程以及他是如何做到的....加上注射范围现在还不能正常工作,这很烦人。提前致谢。

0 个答案:

没有答案