【发布时间】:2017-05-22 17:28:21
【问题描述】:
我正在尝试测试只有一个功能的 geocode.js:
// geocode.js
const googleApiClient = require('@google/maps').createClient({
key: CONFIG.googleApis.key
});
const poll = zipcode => {
return new Promise((resolve, reject) => {
googleApiClient.geocode({
address: zipcode
}, function(err, response) {
resolve(response.json.results); // This is line 22
});
});
};
module.exports = {poll}
所以我已经设置了我的 mocha 环境并安装了 sinon。我不知道如何存根 googleApiClient 功能。
我实际上不想在测试期间拨打任何类型的外部电话。
//geocode.spec.js
describe('geocode', function(){
before(function(){
//HOW DO I STUB googleApiClient ?
sinon.stub ...
})
});
【问题讨论】:
-
替代 POV:不要模拟
googleApiClient,而是将客户端作为参数传递给poll。然后,您可以使用geocode方法创建任何普通的旧对象,而不必对googleApiClient对象进行猴子补丁。