我正在为使用摩卡/茉莉花的服务编写单元测试。我的原始服务依赖于NodeSrv服务。然而,当我将它注入到我的单元测试中时,它看起来并不是真正地注入依赖NodeSrv服务。我得到了TypeError: Cannot read property 'spyOn' of null
describe("Label Exists Check Service", function() {
var LabelExistsCheck;
var NodeSvc;
var VipSvc;
beforeEach(function() {
return module("main");
});
beforeEach(inject(function(_LabelExistsCheck_, _NodeSvc_, _VipSvc_) {
LabelExistsCheck = _LabelExistsCheck_;
NodeSvc = _NodeSvc_;
VipSvc = _VipSvc_;
}));
describe("It should check if node label exists", function() {
spyOn(NodeSvc, "getNodes").and.returnValue(["testing1", "foo"]);
newLabelName = "testing1";
oldLabelName = "nada";
devices = NodeSvc.getNodes();
deviceExist = devices.some(function(element) {
if (newLabelName == element) {
return true
}});
//spyOn(form, "$setValidity");
it("node label should already exist and call set form", function() {
expect(NodeSvc.getNodes).toHaveBeenCalled();
});
});
});更新:
尝试了下面的方法,得到了TypeError: Cannot read property 'returnValue' of undefined
describe("Label Exists Check Service", function() {
var LabelExistsCheck;
var NodeSvc;
var VipSvc;
beforeEach(function() {
return module("main");
});
beforeEach(inject(function(_LabelExistsCheck_, _NodeSvc_, _VipSvc_) {
LabelExistsCheck = _LabelExistsCheck_;
NodeSvc = _NodeSvc_;
VipSvc = _VipSvc_;
}));
beforeEach(function() {
spyOn(NodeSvc, "getNodes").and.returnValue(["testing1", "foo"]);
});
it("It should check if node label exists", function() {
newLabelName = "testing1";
oldLabelName = "nada";
devices = NodeSvc.getNodes();
deviceExist = devices.some(function(element) {
if (newLabelName == element) {
return true
}});
//spyOn(form, "$setValidity");
});
});发布于 2014-10-20 02:07:29
.andReturn的旧语法似乎起作用了。不确定我的系统中有什么坏了,因为我正在/仍在使用Jasmin2.0。
更新:在testem配置中.我需要指定jasmine2,即使没有安装茉莉<2。
https://stackoverflow.com/questions/26457176
复制相似问题