简体   繁体   English

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

[英]How to inject services in directives testing

I have a directive which has a dependency on one of the services. 我有一个对其中一项服务有依赖性的指令。 It has a method called getcustomers(). 它有一个称为getcustomers()的方法。 While I am trying to test the directive How do I inject that service? 在尝试测试指令时,如何注入该服务?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM