目前单元测试一个包含this.refs [data]的函数。因为我只能做浅渲染,所以我一直都没有定义。我试图通过
来嘲弄sinonvar refs = { '001m000000RkzHuAAJ': function () {} };
var refStub = sinon.mock(refs);
但是我相信因为这个函数正在做这个.refs而不是refs它不起作用。有关如何模拟this.refs的想法吗?
编辑:
const wrapper = shallow (
<CashAllocation receipt={receipt} slds={''} clients={clients} billingStatements={billingStatementswithAllocation} userTitle={'Premium Accounting User'} userPermission={2} />
);
wrapper.instance().autoApply();
wrapper.update();
console.log(wrapper.state('billingStatements'));
它在呼唤什么:
autoApply() {
const clientsSortedbyLargestOutstanding = this.props.clients.slice().sort(function(a,b) {
if (a.outstandingBalance < b.outstandingBalance) {
return 1;
} else if (a.outstandingBalance == b.outstandingBalance) {
return 0;
} else {
return -1;
}
});
var amountToApply = this.state.receipt.Amount_Remaining__c;
clientsSortedbyLargestOutstanding.forEach(function(client) {
var amountApplied = 0;
if (this.state.receipt.Amount_Remaining__c == 0) {
amountToApply = 0;
} else {
if (client.outstandingBalance < (amountToApply + client.currentAllocation)) {
amountApplied = client.currentAllocation + (client.outstandingBalance - client.currentAllocation);
amountToApply = amountToApply - (client.outstandingBalance - client.currentAllocation);
} else {
amountApplied = client.currentAllocation + amountToApply;
amountToApply = 0;
}
}
this.refs[ client.Id ].autoApply(amountApplied);
}, this);
}
我收到以下错误。当我通过堆栈跟踪它的this.refs导致它:
TypeError: Cannot read property 'autoApply' of undefined