2014-11-22 24 views
0

在這段代碼:Jasmine-對象屬性TOBE()錯誤

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); 
    }); 
+0

您是否曾嘗試在您的「beforeEach」回調中添加'console.log()'? – Pointy 2014-11-22 21:18:09

回答

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 {}的服務。