我有两个文件 - cloud utils.js 和 processor.js 。我正在 processor.js 文件中导入 utils.js 文件
如何在节点js应用程序中使用sinon为以下代码编写单元测试用例。
utils.js
for distro in jsonData:
print(jsonData[distro]) # This is the value of dict "jsonData" at key "distro"
processor.js
exports.create = function(applicationId, password) {
return new sdk(applicationId, password);
}
function sdk(applicationId, password) {
this.appId = applicationId;
this.password = password;
this.serverUrl = "https://cloud.com";
}
sdk.prototype.processImage = function(filePath, settings, userCallback) {
console.log('here at processImage');
}
现在,我必须为 processor.js 文件和间谍import CloudUtils from '../lib/sdk';
export default class CLOUD {
constructor() {}
getCloudData = async(payload) => {
var sdk = CloudUtils.create(appId, password);
sdk.processImage(input, settings, this.uploadCompleted);
}
}
方法的getCloudData()
方法编写测试用例。
答案 0 :(得分:0)
我很难用babel运行代码,因此使用非ES代码。测试是检查processImage是否至少被调用过一次,如果是,则断言返回的值是否为“云”#。
要使测试通过,只需传递值' cloud'在存根调用中。
// a.js
var B=require('./b');
class CLOUD {
getCloudData(){
var b = new B();
return b.processImage();
}
}
module.exports = CLOUD;
// b.js
class cloudutils {
processImage(){
return 'cloud';
}}
module.exports = cloudutils;
//测试文件
var A=require('./a'),B=require('./b')
var sinon=require('sinon');
var assert=require('assert');
describe("Test", () => {
var InstanceOfB;
var InstanceOfA;
beforeEach(() => {
InstanceOfB = new B();
InstanceOfA = new A();
});
it('should call processImage at least once', () => {
sinon.stub(B.prototype, 'processImage', () => 'mock');
let res = InstanceOfA.getCloudData();
sinon.assert.calledOnce(B.prototype.processImage);
assert.equal(res,'cloud');
});
});