gpt4 book ai didi

angularjs - Jasmine - TypeError : Cannot read property 'spyOn' of null

转载 作者:行者123 更新时间:2023-12-01 10:45:41 28 4
gpt4 key购买 nike

我正在为使用 mocha/jasmine 的服务编写单元测试。我的原始服务依赖于 NodeSrv 服务。但是,当我在单元测试中注入(inject)它时,它看起来并没有真正注入(inject)依赖项 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");

});
});

最佳答案

.andReturn 的旧语法似乎可以代替。不确定我的系统有什么问题,因为我曾经/仍在使用 Jasmine 2.0。

更新:在测试配置中...即使未安装 jasmine < 2,我也需要指定 jasmine2

关于angularjs - Jasmine - TypeError : Cannot read property 'spyOn' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26457176/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com