Jasmine - TypeError: Cannot read property 'spyOn' from null

I am writing a unit test for a mocha / jasmine service. My initial service is service dependent NodeSrv

. However, when I inject it into my unit test it doesn't seem like it is actually injecting a dependency service NodeSrv

. I getTypeError: 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();
    });
  });
});

      


UPDATE:

Tried below and got 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");

  });
});

      

+3


source to share


1 answer


The old syntax seems to work instead .andReturn

. Not sure what's broken on my system since I am / still using Jasmine 2.0.



UPDATE: in testem config ... I need to specify jasmine2

even if jasmine <2 is not set.

+6


source







All Articles