简体   繁体   English

模拟链接方法

[英]Mocking chained methods

(I use Jest for testing) For example, I have this function: (我使用Jest进行测试)例如,我具有以下功能:

const find = () => {
    return {
        where: () => {
            in: () => {}
        }
    };
};

and I call that in different place: 我在另一个地方叫它:

find('me').where('id').in(['123']);

How to mock and test calls in find(), where() and in()? 如何在find(),where()和in()中模拟和测试调用?

Here's a dirt simple mock interface: 这是一个简单的模拟接口:

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM