如何用 sinon 存根模块方法的回调?
How do I stub a module method's callback with sinon?
我正在使用 mv 模块移动文件,这是一个较旧的模块,仍然有很多下载,并且有助于处理 docker 卷之间以及 docker 之外的文件移动].我创建了一个将 mv 方法视为承诺的方法。
进行单元测试时,我很难在方法中使用时模拟 mv。
import mv from 'mv';
export class CmdUtils {
/**
* Move a file to a new location. Using mv, this will attempt a mv command, however
* this could fail with docker volumes and will fallback to the mv module streaming
* data to the new location as needed.
* @param filePath - Full path to file
* @param destinationFilePath - Full path to file's new destination
*/
public static moveFile = (filePath: string, destinationFilePath: string): Promise<void> => {
return new Promise((resolve, reject) => {
mv(filePath, destinationFilePath, err => {
if (!err) {
resolve();
} else {
console.error('moveFile err', err);
reject(err);
}
});
});
}
}
这是一个有差距的开始测试:
import mv from 'mv';
import { CmdUtils } from './cmd-utils';
describe("CmdUtils: moveFile", () => {
it("should successfully move a file using mv module", async () => {
const sandbox = sinon.createSandbox();
try {
const moveFromPath = '/full/path/to/file.jpg';
const moveToPath = '/full/path/to/destination/file.jpg';
// Starting attempt at a replacement callback
const mvCallback = (err: Error) => {
if (!err) {
Promise.resolve();
} else {
console.error('moveFile err', err.message);
Promise.reject(err);
}
}
// Stub mv
sandbox.stub(mv)
// What am I missing here?
// Move file
await CmdUtils.moveFile(moveFromPath, moveToPath);
// expect no errors... (will revisit this)
} finally {
sandbox.restore();
}
});
});
如何模拟成功的文件移动?我看到了类似的问题,但还没有找到答案。
class CmdUtil {
static moveFileCb(resolve, reject, err) {
if (!err) {
resolve('RESOLVED');
} else {
reject(err);
}
}
static moveFile() {
return new Promise((resolve, reject) => {
mv('test', 'test', CmdUtil.moveFileCb.bind(this, resolve, reject))
})
}
}
然后测试可能类似于您现在存根 moveFileCb
处的测试。使用该存根,您可以 check/assert 调用回调的内容、调用次数等。
旁注:我没有使用 bind
编写测试。这可能会给解决方案带来麻烦。
我正在使用 mv 模块移动文件,这是一个较旧的模块,仍然有很多下载,并且有助于处理 docker 卷之间以及 docker 之外的文件移动].我创建了一个将 mv 方法视为承诺的方法。
进行单元测试时,我很难在方法中使用时模拟 mv。
import mv from 'mv';
export class CmdUtils {
/**
* Move a file to a new location. Using mv, this will attempt a mv command, however
* this could fail with docker volumes and will fallback to the mv module streaming
* data to the new location as needed.
* @param filePath - Full path to file
* @param destinationFilePath - Full path to file's new destination
*/
public static moveFile = (filePath: string, destinationFilePath: string): Promise<void> => {
return new Promise((resolve, reject) => {
mv(filePath, destinationFilePath, err => {
if (!err) {
resolve();
} else {
console.error('moveFile err', err);
reject(err);
}
});
});
}
}
这是一个有差距的开始测试:
import mv from 'mv';
import { CmdUtils } from './cmd-utils';
describe("CmdUtils: moveFile", () => {
it("should successfully move a file using mv module", async () => {
const sandbox = sinon.createSandbox();
try {
const moveFromPath = '/full/path/to/file.jpg';
const moveToPath = '/full/path/to/destination/file.jpg';
// Starting attempt at a replacement callback
const mvCallback = (err: Error) => {
if (!err) {
Promise.resolve();
} else {
console.error('moveFile err', err.message);
Promise.reject(err);
}
}
// Stub mv
sandbox.stub(mv)
// What am I missing here?
// Move file
await CmdUtils.moveFile(moveFromPath, moveToPath);
// expect no errors... (will revisit this)
} finally {
sandbox.restore();
}
});
});
如何模拟成功的文件移动?我看到了类似的问题,但还没有找到答案。
class CmdUtil {
static moveFileCb(resolve, reject, err) {
if (!err) {
resolve('RESOLVED');
} else {
reject(err);
}
}
static moveFile() {
return new Promise((resolve, reject) => {
mv('test', 'test', CmdUtil.moveFileCb.bind(this, resolve, reject))
})
}
}
然后测试可能类似于您现在存根 moveFileCb
处的测试。使用该存根,您可以 check/assert 调用回调的内容、调用次数等。
旁注:我没有使用 bind
编写测试。这可能会给解决方案带来麻烦。