![](/img/trans.png)
[英]Node.js axios unit testing using Mocha, sinon and chai
[英]unit testting using sinon+mockery+chai in node.js
我對單元測試非常陌生,因此請指導我完成以下工作:我正在嘗試對以下功能進行單元測試...
helpers.js
function helpers() {
}
helpers.prototype.Amount = function(callback){
var Amount = 0;
app.models.xxx.find({}, function(err, res) {
if(err){
} else {
for(var i=0; i < res.length; i++){
Amount = Amount + res[i].hhhh;
}
return callback(null,Amount);
}
});
}
module.exports.helpers = helpers;
helpers-test.js
describe('helper', function(){
var AmountStub = sinon.stub(Helper.protoype,"getAmount");
it('should return the amount', function(done){
var helper = new Helper();
helper.getAmount(function(err, res){
assert.ifError(err);
});
done();
});
});
但是我收到以下錯誤:
/node_modules/sinon/lib/sinon/util/core.js:67
throw new TypeError("Should wrap property of object");
^
TypeError: Should wrap property of object
請指導我。 我做的方式也是對的嗎? 提前致謝..
編輯:
var Helper = require("../../server/helpers").helpers;
var helper = sinon.stub(
new Helper(),
"getAmount",
function (callback) { callback(1000); }
);
helper.getAmount(
function (value) {
expect(value).to.be.equal(1000);
done();
});
});
根據sinon文檔,您需要傳遞一個對象本身,而不是其原型。
var helper = sinon.stub(new Helper(), "getAmount");
在您的情況下,您想在it
測試中進行存根it
並提供該函數的替代品:
var helper = sinon.stub(
new Helper(),
"getAmount",
function (callback) { callback(dummyValue); }
);
helper.getAmount(
function (value) { done(); }
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.