我有很多服务,比如:
function ServiceA() {}
ServiceA.name = "Service A";
ServiceA.description = "A service called A";
ServiceA.doStuff = new function(inputString){...});
的是:
目前,我对每项服务进行单独测试,检查属性和方法结果。但是,由于测试/测试的内容对于每项服务都是相同的,我想知道我是否可以做这样的事情:
var services = [ServiceA, ServiceB, ServiceC];
for (index = 0; index < services.length; index++) {
describe("Testing Service", testFunction(services[index]));
}
function testFunction(service){
it("bla", function(){...}
}
我使用Karma和Jasmine。
答案 0 :(得分:0)
我设法得到了我想要的东西:
var allServices = [
{ service: ServiceA, expectedResult: "resultA"},
{ service: ServiceB, expectedResult: "resultB"},
{ service: ServiceC, expectedResult: "resultC"}
];
for (index = 0; index < allServices.length; index++) {
var service2test = allServices[index].service;
var expectedResult= allServices[index].expectedResult;
describe("Testing Service", function() {
...
//test properties of service2test
//compare return value of service2test with expectedResult
...
});
}
由于以下错误,制作单独的测试功能并将服务传递给它不起作用:
无法读取未定义的属性调用