Javascript Jasmine-对象属性toBe()不正确
在这段代码中:Javascript Jasmine-对象属性toBe()不正确,javascript,unit-testing,jasmine,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 (n
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;
}
});
}
一切正常,但我无法获得expectdisable.checkBox.toBetrue;工作。我被期望为假而不是真。我已经验证了console.logdisable.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);
});
我需要使用完整的服务名称FormDeviceExistSvc来访问值FormDeviceExistSvc.disable.checkBox
另外,请注意:在测试中,您只想测试什么是公共的,而不是私有的。所以服务中返回{}的任何内容都可以测试 您是否尝试过在每次回调前添加console.log?
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);
});