1

I have a directive whose controller function has _.union method being called.

$scope.arr =  _.union($scope.$parent.arr,$scope.$parent.DDC.arr);

When i run my test cases, all the test-cases are failing because of the above line. If i comment out the above line, everything passes.

Below is my test-case:

describe("directive: Testing Modules", function() {
    // Suite for testing an individual piece of our feature.
    describe('Test Directive', function() {
        var  $compile, scope, elm,directive;

        module('app');
        beforeEach(module('templates'));        
        beforeEach(function() {

            inject(function(_$rootScope_, _$compile_) {
                $compile = _$compile_;
                scope = _$rootScope_.$new();
            });
            inject(function($compile) {
                elm = $compile('<directive></directive>')(scope);
                scope.$digest();
                directive = elm.scope();              
            });
        });


        describe('key date list Directive Initialization Test cases', function(){   

            it('directive should exist', function(){
                expect(directive).toBeDefined();
            })

            it('arr variable should exist', function(){
                expect(directive.arr).toBeDefined();
            })

            it('arr variable should be type of boolean', function(){
                expect(directive.arr).toEqual(jasmine.any(Object));
            })                   

        })
    });
});

In my karma.conf.js, i have included, above my app.js too.

'bower_components/ng-lodash/build/ng-lodash.min.js',
Shane
  • 5,517
  • 15
  • 49
  • 79
  • 'Failing' with what error? The question doesn't contain enough information. – Estus Flask Jul 03 '17 at 15:24
  • @estus: The test-cases are failing with directive => undefined, cannot test property of undefined of undefined... – Shane Jul 03 '17 at 15:28
  • In the code you've posted `directive` variable isn't defined. – Estus Flask Jul 03 '17 at 15:30
  • @estus: updated the code... the problem is when that _.union function is not commented out, my controller scope or directive scope is coming as undefined. – Shane Jul 03 '17 at 15:33
  • There's no `var directive`. It's not possible to help if your actual code differs somehow from the code you're posting. The problem is most likely an error in beforeEach block. Please, post the actual error you're getting. If you're not getting an error from beforeEach block, that's probably because it is swallowed. If you're using Phantomjs, try to replace Phantomjs with Chrome. It's not possible to solve this without having actual error that causes these variables to be undefined. – Estus Flask Jul 03 '17 at 15:40
  • I am using chrome launcher, the actual error i get is Expected undefined to be defined, expect(directive.arr).toBeDefined(); is failing... directive = elm.scope(); does not contain the arr variable, neither elm.controller('directive') which is returning me undefined. The moment i comment out the _.union method in my directive controller... elm.scope() returns me other variables attached to the scope. – Shane Jul 03 '17 at 15:48
  • The 'actual' error is failed expectation. The real error in beforeEach is swallowed. Currently you're the only person who can debug it. If it fails because of this line, this can be because of Lodash OR because `$scope.$parent.DDC.arr` chain doesn't exist. It's not possible to help you without having http://stackoverflow.com/help/mcve or more details. – Estus Flask Jul 03 '17 at 16:03
  • Possible duplicate of [How do I include lodash in my protractor test specs?](https://stackoverflow.com/questions/25467600/how-do-i-include-lodash-in-my-protractor-test-specs) – Paul Sweatte Sep 20 '17 at 15:39

0 Answers0