Tag: 嘲笑

使用node-mocks-httptesting一个服务,没有事件发出

我有一个简单的服务,我想testing,通过调用它的控制器。 我想我会使用node-mocks-http创build一个模拟请求,并查看响应的结果。 然而,尽pipe所有的文档和示例代码,我无法得到响应发出任何事件(“结束”,“发送”,甚至“错误”),因此不知道何时testing输出。 这是一个简单的function(使用Express 4. *): export function getServiceHealth(req, res) { let message = 'service has been up for ' + process.uptime() + ' seconds!'; res.status(200).send(message); } 这是我的testing(茉莉花): import {EventEmitter} from 'events'; import httpMock from 'node-mocks-http'; import {getServiceHealth} from '../../lib/controllers/health/'; describe('Service health integration tests', () => { it('should get health', done => { let req […]

如何模拟来自http.request(nodejs,jasmine,sinon)的响应

我写了一个小的node模块,使得一个http请求,我有麻烦testing它。 有问题的代码如下所示: module.exports = (function () { var http = require("http"), Promise = require("promise"); var send = function send(requestOptions, requestBody) { return new Promise(function (resolve, reject) { http.request(requestOptions, function (response) { var responseChunks = []; response.on('data', function (chunk) { responseChunks.push(chunk); }); response.on('end', function () { resolve(Buffer.concat(responseChunks)); }); response.on('error', function (e) { reject(e); }); }).end(requestBody); }); […]

嘲笑正在testing的方法内部的一个函数

我想testing一个方法在我的代码执行一组不同的function…和其中一个内部函数被调用时,发送电子邮件。 我想要的是避免这个function在运行testing时发送电子邮件。 有没有办法做到这一点? 我想在我的代码中远离类似以下的东西: if (app.get('env') !== 'test') 我使用承诺 ,我想testing的function如下所示: var emailService = require('../path/to/custom/service.js'); // This is the method I want to test exports.sendKeyByEmail = function(email) { return _getByEmail(email) // finds a user given an email .then(function(user) { if (!user) { throw new UserNotFoundError(); } user.key = _generateKey(); // generates a random hash return user.save(); […]

嘲笑os.type()不工作

我试图unit testing一个依赖于底层操作系统的模块。 我试图使用嘲笑模拟os.type()返回基于不同的testing夹具的Linux / Windows_NT。 我正在使用摩卡作为我的unit testing框架。 我有一个描述部分,unit testing我的模块的functionA,其中有2个描述块:一个用于Windows,一个用于Linux。 设置:var reload = require(“require-reload”)(require); var module_we_test = reload('…'); var linuxOsMock = { type: function () { return "Linux"; } }; var windowsOsMock = { type: function () { return "Windows_NT"; } }; describe('#the_module_we_test', function() { before(function() { mockery.enable({ warnOnReplace: false }); }); after(function() { mockery.disable(); }); […]

用jasmine和node.js嘲笑文件系统

我在用茉莉花testing我的文件访问时遇到了麻烦。 我正在写一个简单的监视器,用require('fs').watch注册一个callback函数, require('fs').watch并发出一个包含文件名称的事件,这里没有什么特别的。 但是,当我尝试编写模拟fs模块的testing时,我遇到了一些问题。 这是我的Watcher类(CoffeeScript提前) class Watcher extends EventEmitter constructor: -> @files = [] watch: (filename) -> if !path.existsSync filename throw "File does not exist." @files.push(filename) fs.watchFile filename, (current, previous) -> this.emit('file_changed') 这是我的testing: it 'should check if the file exists', -> spyOn(path, 'existsSync').andReturn(true) watcher.watch 'existing_file.js' expect(path.existsSync).toHaveBeenCalledWith 'existing_file.js' 这个工作正常,通过没有任何问题,但这个完全失败,我不知道我是否正确传递的论据。 it 'should throw an exception if file […]

nodejs / mocha / nock – 模拟整个html响应

我如何为我的testing模拟整个HTML正文响应? 我正在使用nodejs / mocha / nock。 与诺克我可以嘲笑JSON响应就好,例如: nock('http://myapp.iriscouch.com') .get('/users/1') .reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte'}); 我使用curl -o来获取我想要的模拟html,所以我已经在一个文件中 – 但我不知道如何将HTML文件传递给nock(或其他)。 谢谢。

如何模拟通过require创build的对象

我是node.js的新手,在大多数代码中,我没有看到依赖关系的IoC / DI样式构造函数注入。 相反,通常使用node.js扩展require()来创build允许访问外部模块导出的本地variables。 但是当编写unit testing(隔离单层/函数)时,如何模拟通过使用require创build的variables访问的模块? /helpers/dataHelper.js var dataModel = require('../models/dataModel.js'); var getFormattedDataForRegion = function(region, callback) { var data = {}; // validate region // query dataModel // async.map format data items // callback(data); } /tests/dataHelperTests.js describe('dataHelper', function(){ it('getFormattedDataForRegion returns expected response', function(done){ var expectedData = {}; // populate expectedData // **** need some way […]

Node.js:如何testing我的API,嘲笑我的API调用的第三方API

我正在开发一个RESTful Node.js API(express + mongoose)这个API调用第三方Oauth API(谷歌,脸谱,不pipe)。 我一直很高兴使用mocha + chai +请求设置自动化testing,但是我无法模拟第三方API来testing调用它的路由(我的API)。 我已经尝试过使用nock,但是它不适用于我的用例。 为了运行我的testing,我开始我的API( npm start ),并在另一个选项卡中启动testing套件( npm test )。 testing套件使用请求通过HTTPtestingAPI。 因此我认为nock在这里不起作用,因为它在'testing套件'过程中嘲弄http,而不是在'API'过程中。 我绝对需要嘲笑这个第三方调用,原因有两个:1.我希望能够在我的笔记本电脑上运行我所有的testing套件。2.由于第三方API使用Oauth,testing套件中的硬编码证书即使是一个testing帐户)似乎并不太容易。 我真的很喜欢不要在我的testing报道中留下这个巨大的漏洞,所以任何build议都会大大降低!

模拟模块,它在node.js中返回一个函数

我们有一些我们想要testing的node.js代码。 这些是返回一个函数的模块( module.exports = function(){…} )。 在function里面还需要一些其他的模块。 现在我们要模拟这些模块。 看下面的例子: // userRepo.js module.exports = function(connection) { // init the repo var repo = DB.connect(connection); // add validation function repo.validate = function(data, cb) { // do validation stuff cb(error, result); }; return repo; }; // userController.js module.exports = function(config) { var repo = require('userRepo.js')(config.connectionStringToUserDB) var pub = […]

使用fetch-mock模拟zip压缩响应

我试着用下面的代码: require( 'isomorphic-fetch' ); const fetchMock = require( 'fetch-mock' ), fsp = require( 'fs-promise' ), unzip = require( 'unzip' ), rimraf = require( 'rimraf-then' ), path = require( 'path' ); let zipLink = 'https://github.com/stevemao/left-pad/archive/master.zip', out = 'left-pad-master'; // Careful: lib might be removed at any moment. fetchMock.get( zipLink, fsp.createReadStream( path.join( __dirname, 'left-pad-master.zip' ) ) ); […]