lundi 21 décembre 2015

Error: Unexpected request in test after injecting '$state' into a 'run' function

I have a unit test to check a directive (this does no ajax calls). The test was working perfectly, however I have just added a run() function to my main app which injects $state. Now the test errors with "Error: Unexpected request". I don't know why it thinks it is doing any ajax calls as it is just a unit test testing the directive. I know that I can use $httpBackend.whenGET(foo).respond(bar); but I don't want to have to go back and retrofit all my tests with $http mocks, especially when they are unit tests that shouldn't rely on ajax calls. Is there a way around this?

In case there is something in my test which is dependant on $state, I'll add the code below.

describe('MatchLink', function() {
  var $scope = null;
  var $compile = null;

  beforeEach(module('myApp'));

  beforeEach(inject(function(_$rootScope_, _$compile_) {
    $scope = _$rootScope_.$new();
    $compile = _$compile_;
  }));

  describe('Validation', function() {
    var element, form;

    beforeEach(function() {
      $scope.model = {
        first: null,
        second: null
      };
      element = $compile(['<div ng-form="theform">',
        '<input ng-model="first" name="first"/>',
        '<input ng-model="second" name="second" match="first"/>',
        '</div>'
      ].join(''))($scope);
      form = $scope.theform;
    });

    it('has no errors when the inputs are the same', function() {
      form.first.$setViewValue('test');
      form.second.$setViewValue('test');
      $scope.$digest();
      expect(form.second.$valid).toBe(true);
      expect(form.second.$error.match).toBe(undefined);
    });

    it('has errors when the inputs aren\'t the same', function() {
      form.first.$setViewValue('test1');
      form.second.$setViewValue('test');
      $scope.$digest();
      expect(form.second.$valid).toBe(false);
      expect(form.second.$error.match).toBe(true);
    });
  });
});

Aucun commentaire:

Enregistrer un commentaire