如何在指令测试中注入服务



我有一个指令,它依赖于其中一个服务。它有一个名为getcustomers()的方法。当我尝试测试指令时,我该如何注入该服务?

  var app = angular.module('mymod', []);
        app.service('myser', myservicename);
        app.directive('mydir', mydir);
//Before Beforeeach(inject) 
beforeEach(module('mymod'));  //This loads all the necessary dependencies for your directive

spyOn(myservice, 'methodname').andCallThrough();
That's it..above line mocks the service call and lets you call it....

最新更新