dimanche 28 février 2016

How to mock variable in directive unit test?

I have created a custom directive and would like to mock out a variable to make the test working. This is part of the unit test:

it('should pass on initialvalue', function() {
    scope.$apply(function() {
      scope.initial = 2;
      scope.Repairer = null;
    });
    expect(elementScope.initial).toEqual(2);
  });

The directive is calls a service when the initial-variable is set. Both tests are failing because in the directive I have a variable called request that is not properly mocked. The question is how can I mock this out? Or do I need to put the request variable on scope? This is part of the code:

 if (scope.Repairer) {

                        console.log('calling scriptservice');
                        var request = {
                            allocationProcess: (scope.$parent.lodgement.searchSettings.automatic.visible) ? 'automatic' : 'direct',
                            allocationSource: 'internal',
                            brand: brandScriptTag, // lookup brand scripting name
                            includeSegment: false,
                            relationship: scope.repairer.relationshipCode,
                            ruralSearch: scope.repairer.isRural,
                            state: scope.$parent.lodgement.claimLocation

                        };
                        scriptingService.getScript(request).then(function (scripts) {
                            scope.scripts = scripts;
                        });
                    } else {
                        scope.scripts = null;
                    }

plunker ref:http://ift.tt/1n9GTmp

Aucun commentaire:

Enregistrer un commentaire