Tag: 嘲笑

我怎样才能使用JEST间谍来testing一个NodeJs模块/应用程序

我不明白如何侦测一个模块内部的方法/函数是否被触发。 这是我的例子: db.js module.exports.saveUser = (user) => { console.log('Saving the user', user); }; app.js let db = require('./db'); module.exports.handleSignup = (email, password) => { db.saveUser({ email, password }); } app.test.js const db = require('./db'); jest.genMockFromModule('./app'); const app = require('./app'); describe('App: ', () => { it('should call "db.saveUser" with a user object', () => { let […]

configuration模拟cli

我试图使用模拟cli来存储cli应用程序的摩卡testingprocess.arv。 我想testing一个消息是console.logged当一个不正确的参数(“imit”)被传递给process.argv(由命令定义)。 我正在尝试修改文档中的示例,但是我不认为我已经正确设置了所有内容。 它通过时,我注释掉“ stdin: require('../mocks/fakeInputStream'), // Hook up a fake input stream ”虽然我知道它不能正常工作 它与TypeError: sourceStream.on is not a function失败TypeError: sourceStream.on is not a function运行时TypeError: sourceStream.on is not a function ,如下所述 有人可以看到我失踪? /index.js var commands = ['init']; function getGitHeadArgs() { return process.argv.slice(2, process.argv.length); } if (getGitHeadArgs().length) { if (!commands.includes(getGitHeadArgs()[0])) { console.log("Silly Githead! That's not a […]

在node.js中对unit testing中的依赖关系进行存根

我从node.jsunit testing开始,并且一直在研究node.js中最常用的unit testing框架。 我想从最常用的框架开始,使事情变得简单,因为可能有更多的信息。 据许多网站可能是摩卡。 虽然我理解这个模块来进行集成testing,但我并不知道如何使用诸如存根依赖之类的特性来处理它。 我已经看到摩卡不提供模拟/存根function,所以我不知道人们通常如何处理这些问题。 所以我想知道现在最stream行的unit testing模块是什么模块,依存关系……一个简短的例子会很棒。 谢谢

如何模拟Node.js的child_process产卵function?

有没有简单的方法来模拟Node.js的child_process spawn函数? 我有如下的代码,并希望在unit testing中进行testing,而不必依靠实际的工具调用: var output; var spawn = require('child_process').spawn; var command = spawn('foo', ['get']); command.stdout.on('data', function (data) { output = data; }); command.stdout.on('end', function () { if (output) { callback(null, true); } else { callback(null, false); } }); 有一个(certificate和维护)的库,允许我嘲笑spawn电话,让我指定模拟电话的输出? 我不想依靠工具或操作系统来保持testing的简单和隔离。 我希望能够运行testing,而不必设置复杂的testing夹具,这可能意味着很多工作(包括更改系统configuration)。 是否有捷径可寻?

模拟一个模块NodeJs的实例

如何在我正在testing的方法中模拟一个module的实例? 方法示例: var item = require('item'); // module to mock // underTest.js module.exports = { parse: function(model) { return new item(model).parse(); } } 我想嘲笑item模块并声明parse方法已被调用。 我的testing套件使用sinon和mocha任何例子来实现,将不胜感激。

用Sinon嘲笑require()函数

我试图在使用Sinon的testing中嘲讽request-promise 。 据我所知,Sinon嘲笑对象的方法,但request-promise只是返回一个函数。 有没有什么办法来模拟一个必需的function? var rp = require('request-promise'); var User = require('../../models/user'); // this works sinon.stub(User, 'message', function() {}); // This is what I'd like to do to request-promise sinon.stub(rp, function() {}); 我也研究过mockrequire和proxyquire,但我认为他们都遇到类似的问题。

在节点中模拟postgres调用

我有一个类似的代码 const pg = require('pg'); const async = require('async'); const conn = 'pg://username:pass@my_db:2435/db'; const client = new pg.Client(conn); exports.handler = function(event, context) { async.waterfall([ query_aggregate(callback), my_next_function(rows, callback) ], function (err) { if (err) { context.fail(err); } else { context.succeed('Succeed'); } }); }; function query_aggregate(callback) { client.connect(function (err) { if(err) callback(err); const query = 'SELECT shop_id, […]

用Jasmine和Express嘲笑请求对象

我有一个非常简单的方法: module.exports = { login: function (username, password, req, res) { authModule.check_login({login: username, pass: password}).then(function (user) { req.session.regenerate(function () { req.session.user = username req.session.userID = user.id req.session.fullName = user.fullName req.session.email = user.email res.redirect('/user') }) }).catch(function (err) { res.render('login', { unsuccessful: true }) }) } } 在我的unit testing中,我试图validation这个方法是否正确设置会话variables。 我一直试图通过使用jasmine.createSpy来嘲笑请求对象,但似乎没有工作,例如: describe('user_login.login', function () { it('sets the session […]

使用Node的serialportcallback模拟/unit testing

我正在为通过串行端口连接的第三方设备创buildWeb界面。 下面的示例代码… var serialport = require("serialport"); var SerialPort = serialport.SerialPort; var sp = new SerialPort("/dev/ttyACM0", { parser: serialport.parsers.readline("\n") }); sp.on("data", function (rawData) { … 我的问题是如何模拟/unit testingsp数据事件…

使用嘲笑模拟fs模块

在我的nodejs服务器上,我想嘲笑我的testing与摩卡。 我最终使用嘲笑,但我真的误解了一个概念。 在我的testing中(我也使用Typescript): // mock for fs var fsMock = { readdir: (path: string) => { return { err: undefined, files: [] } }, writeFile: (path: string, content: string, encoding: string) => { return { err: undefined } }, readFileSync: (path: string, encoding: string) => { return "lol" } }; Mockery.registerMock('fs', fsMock); beforeEach((done) => { […]