我正在使用Sinon
Enzyme
進行測試。我有一個函數,它接收一組對象並將其轉換爲一個新的不同數組。Sinon:測試函數返回值
getContainersByHostId(data) {
return _.chain(data)
.groupBy('hostId')
.toPairs()
.map(currentItem => _.zipObject(['hostId', 'containers'], currentItem))
.value();
}
參數數量:
const containers = [{
id: 'c_01',
hostId: 'h_01',
hostIp: '192.168.1.0',
name: 'Some Container'
}];
結果:
[{hostId: 'h_01',
containers: [{
hostId: 'h_01',
ip: '192.168.1.0',
id: 'c_01',
name: 'Some Container'
}]}];
這工作得很好。但是,我面臨的問題是在單元測試中。所以目前我有這個。
const containers = [{
id: 'c_01',
hostId: 'h_01',
hostIp: '192.168.1.0',
name: 'Indigo Container'
}];
const wrapper = shallow(<Groups {...props} />);
const instance = wrapper.instance();
sandbox.stub(instance, 'getContainersByHostId');
instance.getContainersByHostId(containers);
expect(instance.getContainersByHostId.calledWith(containers)).to.equal(true);
});
如何測試傳遞的參數是否等於新數組?
更新:
我試圖returnValue
,但它給了我假的,我無法找到任何可能的解決方案,以檢查它真的回來了。
你想要聲明什麼?我想你應該有2個數組,一個用於輸入,另一個用於預期的輸出。所以你只是斷言用該輸入調用'getContainersByHostId'應該返回期望的輸出。這是你想實現的嗎? –
這正是我想要斷言的。但是當我對輸出數組聲明它時,它給了我未定義的內容。你可以寫一個例子來說明我是如何做到的。也許我做錯了什麼。我也會編輯我的問題 – Umair
@LazarevAlexandr – Umair