(我使用 Jest 进行测试) 例如我有这样的功能:
const find = () => {
return {
where: () => {
in: () => {}
}
};
};
我在不同的地方称之为:
find('me').where('id').in(['123']);
如何模拟和测试 find()、where() 和 in() 中的调用?
最佳答案
这是一个非常简单的模拟界面:
const find = (findData) => {
const data = {
find: findData
};
const self = {
where: (whereData) => {
data.where = whereData;
return self;
},
in: (inData) => {
data.in = inData;
return self;
},
data
};
return self;
};
const res = find('me').where('id').in(['123']);
console.log(res.data);
关于javascript - 模拟链式方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51643469/