Jasmine- Object属性toBe()不正确

时间:2014-11-22 21:13:38

标签: javascript unit-testing jasmine

在这段代码中:

var disable = { inputLabelBox : false, nodeIpInput: false, checkBox: false};

  var checkDeviceNameExists = function(devices, form) {
    return devices.some(function(element) {
        nameExists = deviceNameMap(form, element);
        if (nameExists) {
            form.address.$setViewValue(nameExists);
            form.address.$render();
            if (!disable.checkBox || !disable.nodeIpInput) {
                disable.checkBox = true;
                disable.nodeIpInput = true;
                checkBox.checked = false;
            }
            console.log(disable.checkBox);
            return true;
        } else {
            return false;
        }
    });
  }

一切都按预期进行,但我无法让expect(disable.checkBox).toBe(true);工作。我得到了Expected false to be true。我已经确认console.log(disable.checkBox);是真的。有什么建议吗?

  beforeEach(function() {
    form = {};
    form.label = {};
    form.address = jasmine.createSpyObj("address", ["$setValidity", "$setViewValue", "$render"]);
    form.$setValidity = jasmine.createSpy("$setValidity");
    disable = { inputLabelBox : false, nodeIpInput: false, checkBox: false};
  });
  it("It should populate ip address because Vip label pre-exists", function() {
    form.label.$viewValue = "myvip";
    FormDeviceExistSvc.checkDeviceExists(form, "checkForVips");
    expect(VipSvc.getVips).toHaveBeenCalled();
    expect(form.address.$setViewValue).toHaveBeenCalledWith("10.11.11.1");
    expect(disable.checkBox).toBe(true);
  });

1 个答案:

答案 0 :(得分:0)

  it("It should populate ip address because Vip label pre-exists", function() {
    form.label.$viewValue = "myvip";
    FormDeviceExistSvc.checkDeviceExists(form, "checkForVips");
    expect(VipSvc.getVips).toHaveBeenCalled();
    expect(form.address.$setViewValue).toHaveBeenCalledWith("10.11.11.1");
    expect(FormDeviceExistSvc.disable.checkBox).toBe(true);
    expect(FormDeviceExistSvc.disable.nodeIpInput).toBe(true);
    expect(FormDeviceExistSvc.checkBox.checked).toBe(false);
  });

我需要使用完整服务名称FormDeviceExistSvc来访问值FormDeviceExistSvc.disable.checkBox

另外,请注意:在测试中,您只想测试什么是公共的,而不是私有的。所以......可以测试来自return {}服务的任何内容。