Tag: unit testing

testing驱动的节点(没有testing框架)

我有我的代码: var User = function() { … } 和使用IIFE的testing代码: (function() { // test new user var user = new User(); if (typeof user === 'undefined') { console.log("Error: user undefined"); } … }()); 都在同一个js文件中。 太棒了! 但是,随着程序的增长,对于我来说pipe理变得太难了,因为我对每一个业务逻辑都有一个testing代码。 我已经被教导把所有的js都保存在同一个文件中,但是在开发过程中有没有一种最好的方法来保存我的testing代码到不同的文件中呢? 我想我可以在运行testing时使用shell脚本将testing代码附加到生产代码,但是我更喜欢跨平台的解决scheme。 我不想或不需要一个框架解决scheme,我想保持轻量级 – 节点是否有内置的这种东西?

Angular 2testing不能加载自定义包

我正在按照官方的angular2指南对现有项目进行testing。 我使用自定义库, downloadjs ,当我运行该应用程序时,它工作得很好。 但是在testing运行的情况下,在控制台中出现错误: "__zone_symbol__zoneAwareStack": "Error: (SystemJS) XHR error (404 Not Found) loading node_modules/downloadjs/download.js\n\tError: XHR error (404 Not Found) loading node_modules/downloadjs/download.js\n\tError loading node_modules/downloadjs/download.js as \"downloadjs\" from app/utilities/file-handler.utility.js" 我用npm install downloadjs来获得这个工具。 file-handler.utility.js如下: import {Injectable} from "@angular/core"; import downloadjs=require("downloadjs"); @Injectable() export class FileHandler{ public static download(fileToDownload: any) { downloadjs(fileToDownload, "filename.txt" ,"text/plain"); } } 我在同一个文件夹中创build了一个defs.spec.ts文件: declare module […]

使用摩卡混合同步和asynchronoustesting

我有一个函数,计算一些东西,通过callback关于一些事件通知用户: function returnAndCallback(callback) { callback(5); // not always invoked return 3; } 使用Mocha和Should.js,我写了这个testing: describe('mixing sync and async code', function() { it('should test callback AND return value', function(done) { returnAndCallback(function(value) { value.should.be.eql(5); done(); }).should.be.eql(4); }); }); 这是成功的,因为testingdone()时done()被调用。 看来,我可以写一个同步testing,并检查返回值,或者我可以写一个asynchronoustesting,并检查callback。 不能使用像这样的同步testing: describe('mixing sync and async code', function() { it('should test callback AND return value', function() { returnAndCallback(function(value) { […]

如何configuration套接字io和套接字io客户端

美好的一天 我需要通过单位的服务器连接大量的PC到主服务器 我有东西,但我没有完成 主服务器 socketIo = require("socket.io"), ioServer = socketIo(server), ioServer.sockets.on("connection",function(socket){ // Display a connected message console.log("Server-Client Connected!"); // When we receive a message… socket.on("message",function(data){ // We got a message… I dunno what we should do with this… console.log(data); console.log(data.from + " is connected with ip " + data.ip); socket.emit('message', { 'from': '10.19.17.101', 'answer':'I already […]

TDD先testingNodejs表示Rest Api – unit testing中间件/控制器/路由

我想弄清楚如何testing我的节点jsrestAPI应用程序。 到目前为止,我一直在使用nock拦截和模拟任何http调用,并通过testing我的服务作为一个组件。 (组件testing?)我想开始unit testing我的应用程序,所以我的testing金字塔更平衡,testing将更容易编写。 searchnetworking我得到了这种做法: http : //www.slideshare.net/morrissinger/unit-testing-express-middleware var middleware = require('./middleware'); app.get('example/uri', function (req, res, next) { middleware.first(req, res) .then(function () { next(); }) .catch(res.json) .done(); }, function (req, res, next) { middleware.second(req, res) .then(function () { next(); }) .catch(res.json) .done(); }); (基本上拉出中间件并testing它) 因为这个演示文稿是从2014年我想知道什么是当前最新的unit testing快递应用程序的方法?

在Mocha中使用EJStesting渲染视图

我已完成20个testing通过构build一个CRUD应用程序。 我正在使用EJS来渲染视图,现在我的testing有问题。 作为一个简单的例子,假设我正在检查是否发送了一个GET请求发送给'/' ,JSON将被发送像res.json({message:'hello'})所以我可以设置我的testing在摩卡这样res.body.message相当于'你好'。 但是,如果我想呈现index.ejs页面而不是发送JSON。 所以它看起来像res.render('index',{message:'hello'}) 。 我如何testingres.render() ? 或者更具体地说,我如何testing传递给res.render()的对象? 编辑:解决这个问题(和这种问题)可以通过使用称为functiontesting的testing范例解决。 去谷歌上查询。

unit testing模块在承诺解决时调用私有函数

我是Node的unit testing新手,在承诺方面遇到了障碍。 我的模块通过ApiController执行search,然后返回一个promise。 根据其parsing状态,它调用模块中的两个私有函数之一。 模块: module.exports.process = function(req, res) { let searchTerm = req.query.searchValue; ApiController.fetchResults(searchTerm) .then((results) => onReturnedResults(results), (err) => onNoResults()); function onReturnedResults(results) { req.app.set('searchResults', results); res.redirect('/results'); } function onNoResults() { res.render('search/search', { noResultsFound: true }); } }; testing: var res = { viewName: '', data : {}, render: function(view, viewData) { this.view = view; […]

nodejs – testing失败,但callback被调用

我有一个模块,我出口,其中有一个方法editHeroImage我试图用mocha , chai和sinon 。 模块有两个作为参数, connection和queries传递的对象。 这些是mySql对象,其中一个包含到数据库的连接,另一个包含在其单独模块中定义的查询string。 我正在导出并试图testing的expObj是一个“帮手”模块。 我已经成功地testing了这个模块的其他方法,就像我试图testing这个方法一样,但是当我遇到由于某种原因而使用async模块的方法时,我的testing不再像预期的那样工作。 我想知道是否在这种特殊情况下丢失了某些东西,因为我testing了其他模块和方法,这些模块和方法也使用了async ,并没有遇到这种情况。 当我运行testing时,它会logging“HELLO!” 如预期的那样,但是已经调用callbackSpy的断言失败。 我在这里失去了主意! 请帮忙! 到底是怎么回事? 试衣服之间是否会有污染? testing方法: expObj.editHeroImage = function(connection, queries, postId, postData, callback) { async.waterfall([ function(next) { var qString = queries.getSinglePostById(); connection.query(qString, [postId], function(err, results) { if (err) { return next(err); } if (!results.length) { console.log('NO POST FOUND WITH ID ' + postId); […]

Nodejs摩卡unit testingsqlite3callback未调用插入未执行

我想为我的应用程序创buildunit testing。 当通过浏览器调用API时,API工作得很好,但是当我想要执行testing时,无论是使用npm test还是mocha test,都不会调用callback函数,也不会执行INSERT语句。 我没有错误。 码: unit testing文件: process.env.NODE_ENV = 'test'; var chai = require('chai'); var chaiHttp = require('chai-http'); var server = require('../server/server'); var Const = require("../config/const.js"); var should = chai.should(); chai.use(chaiHttp); var assert = chai.assert; var column1value = "1234567890123456" describe('All', function() { it('Insert',function(done) { chai.request(server) .post('/' +Const.SERVER_NAME + '/insert') .set('content-type', 'application/x-www-form-urlencoded') .send({param1: column1value, […]

嘲笑unit testing文件观察器库的文件系统

我目前正在unit testing我在Chokidar周围编写的一个封装器,它是一个文件系统监视器,它本身就是一个本地fs.watchfunction的包装器。 我用摩卡/柴写我的testing。 我知道有这个美好的图书馆模拟fs ,然而,它是说,它是在底部 下面的fs函数目前没有被模拟(如果你的testing使用这些函数,它们将会对真正的文件系统起作用):fs.FSWatcher,fs.unwatchFile,fs.watch和fs.watchFile。 拉请求欢迎。 所以它不会帮助我testing我的观察者。 目前,我已经设置了真正的读/写的FS,没有嘲笑,但它是繁琐的和时间依赖(这使得它依赖于硬件)。 任何人都可以build议我可能更好的方法吗?