Tag: proxyquire

带有proxyquire和sinon的google-geocoder

我还是很学习node,js,sinon,proxyquire等 我有一个使用谷歌地理编码模块( https://github.com/bigmountainideas/google-geocoder )的模块,我正在努力编写一个testing来存根。 这一切都归结为我想你如何设置它。 在time.js中,我按照google-geocoder文档进行如下操作: var geocoder = require('google-geocoder'); … module.exports = function(args, callback) { var geo = geocoder({ key: some-thing }); geo.find('new york', function(err, response) { … }); } 我想要testing如下,但我得到的错误: TypeError: geo.find is not a function at run (cmdsUser/time.js:x:x) at Context.<anonymous> (tests/cmdsUser/time-test.js:x:x) 时间test.js: var time; var findStub; before(function () { findStub = sinon.stub() […]

Stubbing Fetch Call – response.json不会调用

我试图用sinon和sinon-stub-promise一个获取呼叫。 我非常接近…但我不知道为什么它不会调用我创build的response.json()方法。 function toJSON(response) { console.log(response); return response.json(); } function init() { fetch(darkWeatherUrl) .then(toJSON) .then(computeHours) .then(sendAlerts) .catch((e) => { console.log('init error ' + e); }); } describe('lifx alert test', ()=> { it('should run fetch', ()=> { var fetch = sinon.stub().returnsPromise(); var body = { "hourly": { data: [ { time: 1493413200, icon: 'clear-day', precipIntensity: 0, […]

如何绑定一个辅助存根到超级特权的swagger-node?

我用express来使用Swagger Node ,并初始化了框架项目。 Swagger project create hello-world 然后在hello-world/api/controllers/hello_world.js我添加了一个小小的修改,需要一个助手hello_helper.js并调用它的函数helloHelper.getName() 。 'use strict'; let helloHelper = require('../helpers/hello_helper'); var util = require('util'); module.exports = { hello: hello }; function hello(req, res) { var name = req.swagger.params.name.value || helloHelper.getName(); var hello = util.format('Hello, %s!', name); res.json(hello); } 你好世界/ API /助理/ hello_helper.js 'use strict'; module.exports = {getName: getName}; function getName() […]

如何在Knexunit testing时模拟一个假数据库?

我一直使用Knex成功连接到后端数据库。 但是我想能够unit testing我的代码。 有没有办法模拟数据库连接? 我试过使用proxyquire,但我似乎无法得到它的工作。 问题似乎是Knex初始化的方式。 var knex = require('knex')({ client: 'mysql', connection: {} }); 我设置knex在我的unit testing中被嘲笑。 myService = proxyquire('../app/myService', { 'knex': knexProxy }); 我的服务包括knex。 var knex = require('knex').knex, 当我的服务运行查询时,它失败。 var sql = knex("table_name"); sql.insert(rowToInsert, "auto_increment_id"); sql.then(function (insertId) { resolve(); }, function (err) { reject(err); }); 出于某种原因,我似乎无法捕捉请求,然后再尝试连接。 我也尝试创build一个自定义的Knex客户端 ,但是这还没有奏效。

为节点模块映射path,用于unit testing

客户端我用SystemJS将模块path存到模块中,像这样 var systemJsConfig = { baseURL: "./", defaultJSExtensions: true, map: { 'root-components': 'applicationRoot/rootComponents' } }; 所以require('root-components/foo'); 将映射到applicationRoot/rootComponents/foo 。 问题是,如果我用require('root-components/foo');运行一个模块require('root-components/foo'); 在摩卡,节点不知道这个path是什么意思。 是否有一个理想的方式来完成在Node中的path映射? Proxyquire能胜任这个吗? 我阅读他们的文档,但没有发现任何迹象表明它是。 这只是unit testing,所以我很高兴任何解决scheme使用任何forms的党的实用程序。

unit testing使用请求,pipe道和stream使用模拟的私有方法

我想在下面的代码中unit testing导出的方法。 我想嘲笑私有方法中的值来控制返回的Promise的拒绝/parsing。 client是已经连接到数据库的node-postgres对象。 我知道我可以使用proxyquire来取出所需的库,但我怎样才能模仿链接的方法.on('error', …) .pipe(stream)和.on('end', …)我可以控制返回的值。 注意 ,显示的导出方法是对真实导出方法的简化,导出importDomain是不可行的。 const copyFrom = require('pg-copy-streams').from const request = require('request') const Promise = require('bluebird') // private function importDomain (client, domain) { return new Promise((resolve, reject) => { let stream = client.query(copyFrom(`COPY ${domain.table} FROM STDIN;`)) let req = request(`${domain.url}`) req.on('error', reject) req.pipe(stream) .on('error', reject) .on('end', resolve) }) } […]

如何多次声明stubbed的提取

使用proxyquire,sinon和mocha。 我可以在第一次调用fetch时存根。 但是在recursion调用的第二次调用中,我不能断言它。 从输出结果看,在testing结束之前,断言可能会运行。 在断言之后,您将会看到second fetch控制台。 index.js var fetch = require('node-fetch'); function a() { console.log('function a runs'); fetch('https://www.google.com') .then((e) => { console.log('first fetch'); b(); }) .catch((e)=> { console.log('error') }); } function b() { fetch('https://www.google.com') .then((e) => { console.log('second fetch'); }) .catch((e)=> { console.log('error') }); } a() testing: describe('fetch test demo', ()=> { it('fetch should of […]