0

I am trying a SIMPLE directive Unit Test but it is not working. I am getting: Error: Unexpected request: GET my-directive.html No more request expected

Not sure what that means, it works in the web page...

LIVE DEMO: http://plnkr.co/edit/SrtwW21qcfUAM7mEj4A5?p=preview

directiveSpec.js:

describe('Directive: myDirective', function() {

  beforeEach(module('myDirectiveModule'));

  var element;
  var scope;
  beforeEach(inject(function($rootScope, $compile) {
    scope = $rootScope.$new();
    element = angular.element('<my-directive something="thing"></my-directive>');
    element = $compile(element)(scope);
    scope.thing = {name: 'My thing'};
    scope.$digest();
  }));

   it('should update the rendered text when scope changes', function() {
    scope.thing.name = 'My new thing';
    scope.$digest();
    var h1 = element.find('h1');
    expect(h1.text()).toBe('My new thing');
  });
});

app.js:

angular.module('myDirectiveModule', [])
  .directive('myDirective', function() {
    return {
      bindToController: true,
      controller: function() {
        var vm = this;
        vm.doSomething = doSomething;

        function doSomething() {
          vm.something.name = 'Do something';
        }
      },
      controllerAs: 'vm',
      restrict: 'E',
      scope: {
        something: '='
      },
      templateUrl: 'my-directive.html'
    };
  })
  .controller('DirCtrl', ['$scope', function() {
    this.getName = 'Hello world!';
  }]);

How do I simply test a directive unit test?

1 Answer 1

1

You need to mock the template request. Because the directive has the templateUrl it try to make a get request, but the $http doesn't expect any request so it fails. You can mock the request with a response of yours or put the template into the template cache service.

  beforeEach(inject(function($rootScope, $compile, $templateCache) {
    $templateCache.put('my-directive.html','<h1 ng-click="vm.doSomething()">{{vm.something.name}}</h1>');
    scope = $rootScope.$new();
    element = angular.element('<my-directive something="thing"></my-directive>');
    element = $compile(element)(scope);
    scope.thing = {name: 'My thing'};
    scope.$digest();
  }));

Also on your it function call $apply and use $evalAsync. See my forked plunker.

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

6 Comments

But how do I get whatever <my-directive></my-directive> compiles?
Using the $compile service and then calling the $digest method
Didn't I do that in line 5 and line 7 of the code you posted?
Yes you did. But you asked ;-). The problem with the request is describe in the answer
Got it. Thank you. And by response of yours, you mean insert any data for the directive to work with/
|

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.