Reputation: 2456
I am trying to stub functions for my test suite and it is currently not working as expected. I am new to using mocha and sinon and am looking for direction in how to make this work:
Here is a snippet of the code that is being tested which is found in functions/purchaseOrder.js. The AccountStatus, creditStatus and productStatus are local functions within the file:
var orderHandling=function(clientAccount ,product,inventory,inventoryThreshold,creditCheckMode){
var aStautus=AccountStatus(clientAccount);
var cStatus=creditStatus(clientAccount, creditCheckMode);
var pStatus=productStatus(product,inventory,inventoryThreshold);
...more
}
and this is how I am trying to test it:
import testFunctions = require('./functions/purchaseOrder.js');
beforeEach(function() {
stub=sinon.stub(testFunctions, "AccountStatus");
stub1=sinon.stub(testFunctions, "productStatus");
stub2=sinon.stub(testFunctions, "creditStatus"); // stub 'calPoints' function
})
it('Initial Test', function() {
var clientAccount = {
age: 2,
balance: 500,
creditScore: 50
}
stub.onCall(0).returns("very good");
stub1.onCall(0).returns("available");
stub2.onCall(0).returns("good");
var creditCheckMode = 'restricted';
var product = "productname"
var inventory = [{
name: "hello",
productQuantity: 578
}]
var inventoryThreshold = 500
assert.equal(testFunctions.orderHandling(clientAccount, product, inventory, inventoryThreshold, creditCheckMode), "accepted");
});
Thanks in advance
Upvotes: 2
Views: 1888
Reputation: 2456
I have figured out the answer to my question through some digging myself. It turns out that I am trying to stub out the variable that is assigned to the anonymous function it is referencing. Sinon is unable to find this anonymous function and therefore is not stubbing out the method. To fix this I had to change the code to be: var productStatus = {prodStatus: function() {...}
and then stub out the functions like so:
var stub = sinon.stub(testFunctions.productStatus, "prodStatus");
stub.onCall(0).returns("available");
This works perfectly. Hope this helps someone!
Upvotes: 1