我的任务是通过API在数据库中创建帐户。我想对该API进行单元测试,但是我不想访问真实的数据库并创建一个帐户。
我想通过此功能来伪造请求和响应。目前,我可以伪造请求和响应,但无法使用该功能。
const expect = require('chai').expect;
const Acreation = require('../accountsCreation');
var sinon =require('sinon');
describe('account creation Route', function() {
let req;
let res;
beforeEach(function()
{
req = {
body: {
"accountid":"xyz",
"accountname":"newAccount"
}
};
res = {
json:sinon.spy()
}
});
afterEach(function()
{
console.log("done test");
});
it('should return success on sucessful account creation',function(done){
var createAccountStub=sinon.stub(Acreation,'accountsCreation').returns("Success");
Acreation.accountsCreation(req,res);
expect(createAccountStub.called).to.be.true;
expect(createAccountStub.returnValues[0]).to.deep.equal("Success");
done();
})
})