How to you test an angularjs module defined inside an IIFE with jasmine?

独自空忆成欢 提交于 2019-12-05 13:47:15

It can be tested with jasmine simply by doing this :

describe('app', function () {

    var $controller;

    beforeEach(function () {

        module('app');

        inject(function (_$controller_) {

            $controller = _$controller_('MainCtrl');

        });
    });

    //-- spec - test controller

    describe('Controller : MainCtrl', function () {

        it('should create an object with val 50', function () {

            expect($controller.obj.val).toBe(50);

        });
    });

});

here's a jsfiddle for it

Hope it helps !

The problem is here

var scope = {}, ctrl = _$controller_('MainCtrl', {$scope:scope});

You're importing undefined scope. You need to do something like this:

  1. Inject $rootScope : inject(function (_$controller_, _$rootScope_)

  2. Construct new scope: var scope = $rootScope.$new();

  3. Build controller: var ctrl = _$controller_('MainCtrl', {$scope: scope});

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!