Javascript 单元测试调用服务的指令
我有一个查看当前URL并检索querystring参数的服务:Javascript 单元测试调用服务的指令,javascript,angularjs,jasmine,karma-jasmine,angular-directive,Javascript,Angularjs,Jasmine,Karma Jasmine,Angular Directive,我有一个查看当前URL并检索querystring参数的服务: app.service('myService', function($location) { return { getCustID : function() { return $location.search().custID; } }; }); 我已经能够通过以下方式成功地进行单元测试: describe('myService', function(){
app.service('myService', function($location) {
return {
getCustID : function() {
return $location.search().custID;
}
};
});
我已经能够通过以下方式成功地进行单元测试:
describe('myService', function(){
var $location, myService;
beforeEach(module('myApp'));
beforeEach(inject(function (_myService_, _$location_) {
this.myService = _myService_;
$location = _$location_;
}));
it('should get a promoCode from the url', function(){
$location.url('/?custID=DSGAG444355');
expect(this.myService.getCustID()).toEqual('DSGAG444355');
});
});
但是,我有一个使用上述服务的指令。我如何测试它
指令:
app.directive('imageDirective', function($compile, myService) {
return {
restrict: 'A',
replace: true,
scope: true,
link: function (scope, element, attrs) {
var custID = myService.getCustID();
var myText;
if (custID == 3) {
text = 'cust ID is 3';
}
var jqLiteWrappedElement = angular.element('<img src="/resources/img/welcome.png' alt=" ' + myText + '" />');
element.replaceWith(jqLiteWrappedElement);
$compile(jqLiteWrappedElement)(scope);
}
};
});
您可以使用$provide模拟服务
然后按照angular关于如何进行单元测试指令的文档进行操作。使用Jasmine Mock进行单元测试。 下载库后:
describe('mydirective', function(){
var $location, myService;
beforeEach(module('myApp'));
beforeEach(inject(function (_myService_, _$location_) {
myService = _myService_;
$location = _$location_;
spyOn(myService, "getCustID").and.returnValue("123462");
}));
//Write stuff for your directive here
it('should have made a call to ', function(){
expect(myService.getCustId).toHaveBeenCalled()
});
更多参考资料:我认为spyOn线路有错误。。。应该是myService还是this.myService?错误:spyOn找不到GetCustId的可监视对象。很抱歉,this.myService的内容。。刚刚检查过。编辑代码片段,如果不起作用,请尝试在测试块内移动spyOn。它应该可以工作没有问题,按照建议进行了更改,现在的错误是:应该调用spy getCustID。我/你在哪里实际加载指令??这基本上进入了beforeach块。elem=角元素;scope=$rootScope.$new$编译电子显微镜;范围。$摘要@OamPsy您的模拟没有设置函数。你需要指定你希望你的模拟拥有的功能,这些功能可能是真正的服务拥有的所有功能:myMock=jasmine.createSpyObj'myServiceMock',['getCustID',/*等等所有你想要模拟的功能*/]你能创建一个plunker吗?真的没有时间。但是你读过关于如何测试指令的文档了吗?基本上,您需要注入$compile服务并编译您的指令,然后使用一些选择器或其他方法检查是否一切正常。您可能更清楚应该在指令上测试什么
TypeError: myService.getCustID is not a function
var myMock;
beforeEach(module('myApp'));
beforeEach(module(function($provide){
myMock = {}//Mock the service using jasmine.spyObj, or however you want
$provide.factory('myService', function(){
return myMock;
})
}));
describe('mydirective', function(){
var $location, myService;
beforeEach(module('myApp'));
beforeEach(inject(function (_myService_, _$location_) {
myService = _myService_;
$location = _$location_;
spyOn(myService, "getCustID").and.returnValue("123462");
}));
//Write stuff for your directive here
it('should have made a call to ', function(){
expect(myService.getCustId).toHaveBeenCalled()
});