首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >:无法读取属性'spyOn‘的null

:无法读取属性'spyOn‘的null
EN

Stack Overflow用户
提问于 2014-10-20 01:05:36
回答 1查看 4.8K关注 0票数 3

我正在为使用摩卡/茉莉花的服务编写单元测试。我的原始服务依赖于NodeSrv服务。然而,当我将它注入到我的单元测试中时,它看起来并不是真正地注入依赖NodeSrv服务。我得到了TypeError: Cannot read property 'spyOn' of null

代码语言:javascript
复制
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

代码语言:javascript
复制
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");

  });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-20 02:07:29

.andReturn的旧语法似乎起作用了。不确定我的系统中有什么坏了,因为我正在/仍在使用Jasmin2.0。

更新:在testem配置中.我需要指定jasmine2,即使没有安装茉莉<2。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26457176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档