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

时间:2013-02-18 19:47:12

标签: javascript angularjs angularjs-directive

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

1 个答案:

答案 0 :(得分:1)

  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....