vendredi 4 décembre 2015

Trying to understand how to use Angular-mock

All:

I am pretty new to Angular Testing, from its docs: http://ift.tt/1M5fZ4y

There is one example:

angular.module('app', [])
.controller('PasswordController', function PasswordController($scope) {
  $scope.password = '';
  $scope.grade = function() {
    var size = $scope.password.length;
    if (size > 8) {
      $scope.strength = 'strong';
    } else if (size > 3) {
      $scope.strength = 'medium';
    } else {
      $scope.strength = 'weak';
    }
  };
});

And the test specs:

describe('PasswordController', function() {
  beforeEach(module('app'));

  var $controller;

  beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
  }));

  describe('$scope.grade', function() {
    it('sets the strength to "strong" if the password length is >8 chars', function() {
      var $scope = {};
      var controller = $controller('PasswordController', { $scope: $scope });
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
  });
});

I wonder what did Angular-Mock do, why we can directly use those function like "inject", where did we include that module(or did it bind that function to WINDOW global object when loading?)? And how does that inject function tell if that injection is for a controller or directive and etcs, I find they all use same inject function.

Aucun commentaire:

Enregistrer un commentaire