3

My controller method looks like this:

 angular.module(_appName_)

.controller('myController', function ($scope, $rootScope) {
    $rootScope.$broadcast('myObj', false);
    ......some code here.......
});

Jasmine test for testing call made to $rootScope.$broadcast looks like this:

describe("myController",function(){
    var scope,rootScope;

    beforeEach(angular.mock.inject(function($rootScope) {
        scope = $rootScope.$new();
        rootScope = $rootScope;           
    }));

    describe('myController', function() {
        it('rootScope broadcast called for myObj with false value', inject(function($controller, $rootScope) {

            var requestObj = '{"key":"1234567890"}';
            rootScope.requestObject = requestObj;

            $controller('myController', {
                $scope: scope,
                $rootScope: rootScope
            });


            spyOn($rootScope, '$broadcast').and.callThrough();

            expect($rootScope.$broadcast).toHaveBeenCalled();

        }));
    });
});

It always gives me the following error:

Expected spy $broadcast to have been called. at Object.

When i try to put a breakpoint on the line where there is a call to broadcast in the controller method, it does hit the breakpoint while debugging. So the actual call is being made but the test doesn't recognize it somehow.

Can someone please let me know what am I missing here ?

1
  • What if you remove the inner describe and put the it directly after the beforeEach in the outer describe? Commented Mar 30, 2016 at 2:36

1 Answer 1

1

I think you forgot to include your module in beforeEach function.

And then make sure you mock your spyOn($rootScope, '$broadcast') before you initialize your controller

$controller('myController', {
                $scope: scope,
                $rootScope: rootScope
            });

Here is a plunker. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.