Tag: unit testing

Karmaunit testing:模块名称“react”尚未加载上下文:_。 使用require()

我正在尝试设置unit testing框架进行反应。 这样做时发生以下错误。 我已经search了所有的互联网没有解决scheme正在工作。 下面是显示的错误和我正在使用的代码/包。 debugging错误 04 03 2016 04:48:46.340:DEBUG [phantomjs.launcher]: Error: Module name "react" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded 04 03 2016 04:48:46.341:DEBUG [phantomjs.launcher]: http://localhost:9876/base/node_modules/requirejs/require.js?6f53c895855c3743ac6fb7f99afc63ca5cdfd300:140 in defaultOnError http://localhost:9876/base/node_modules/requirejs/require.js?6f53c895855c3743ac6fb7f99afc63ca5cdfd300:544 in onError http://localhost:9876/base/node_modules/requirejs/require.js?6f53c895855c3743ac6fb7f99afc63ca5cdfd300:1429 in localRequire http://localhost:9876/base/node_modules/requirejs/require.js?6f53c895855c3743ac6fb7f99afc63ca5cdfd300:1791 in requirejs PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR Error: Module name "react" has […]

在单独的文件中testingMongoose模型会导致问题(使用Mockgoose&Lab)

每当一个Mongoose模型在加载之后试图加载,就会抛出一个错误,例如: 错误:uncaughtException:编译后无法覆盖Account模型。 date = Fri Feb 26 2016 10:13:40 GMT-0700(MST),pid = 19231,uid = 502,gid = 20,cwd = / Users / me / PhpstormProjects / project,execPath = / usr / local / Cellar / node / 0.12.4 / bin / node,version = v5.2.0,argv = [/ usr / local / Cellar / node / 0.12.4 / bin […]

如何testingnode.js websocket服务器?

我正在使用标准configuration的sockjs。 var ws = sockjs.createServer(); ws.on('connection', function(conn) { conn.on('data', function(message) { wsParser.parse(conn, message) }); conn.on('close', function() { }); }); var server = http.createServer(app); ws.installHandlers(server, {prefix:'/ws'}); server.listen(config.server.port, config.server.host); wsParser.parse函数是这样工作的: function(conn, message) { (…) switch(message.action) { case "titleAutocomplete": titleAutocomplete(conn, message.data); break; (…) // a lot more of these } 交换机中调用的每个方法都会向客户端发回消息。 var titleAutocomplete = function(conn, data) { redis.hgetall("titles:"+data.query, […]

未能通过风帆运行摩卡testing

我正在关注Sails文档并尝试运行Mochatesting。 我已经按照文档指定的方式编辑了我的package.json ,但由于某种原因,当我尝试运行Mocha时,总是得到EACESS,权限被拒绝。 起初我得到了: Error: EACCES, permission denied '/Library/Application Support/Apple/ParentalControls/Users' 我不明白为什么要做运行我的testing,但增加了这个文件夹所需的权限。 然后我得到: Error: EACCES, permission denied '/Library/Application Support/ApplePushService' 再次,不明白,所以改变了这个文件夹的权限,这也没有帮助。 我不明白为什么摩卡需要这些文件的权限,或者如何解决这个问题。 我跑了这个命令: mocha test/bootstrap.test.js test/unit/**/*.test.js 而且我的项目结构和Sails教程完全一样。 我正在使用Mocha@2.2.5 。 我的同事克隆回购,并试图在他的机器上运行testing,但没有完全相同的错误失败。 我试图降级到Mocha@2.2.0这也没有帮助。 完整的错误追踪: events.js:85 throw er; // Unhandled 'error' event ^ Error: EACCES, permission denied '/Library/Application Support/ApplePushService' at Error (native) at Object.fs.readdirSync (fs.js:761:18) at Glob._readdir (/usr/local/lib/node_modules/mocha/node_modules/glob/glob.js:609:20) at Glob._process […]

用Jest(和mockgoose)testingNode.js API

这里有两个问题: 1)Jest是testingNode.js(express)API的好select吗? 2)我试图用Jock与Mockgoose ,但我不知道如何build立连接和运行后的testing。 这是我来之前的最后一次尝试: const Mongoose = require('mongoose').Mongoose const mongoose = new Mongoose() mongoose.Promise = require('bluebird') const mockgoose = require('mockgoose') const connectDB = (cb) => () => { return mockgoose(mongoose).then(() => { return mongoose.connect('mongodb://test/testingDB', err => { if (err) { console.log('err is', err) return process.exit() } return cb(() => { console.log('END') // this is […]

Cloud9中的Mochatesting中的“未声明的variables”警告

我是node.js和unit testing框架Mocha的新手,但是我已经在cloud9 IDE中创build了一些testing,以了解它是如何工作的。 代码如下所示: var assert = require("assert"); require("should"); describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when the value is not present', function(){ assert.equal(-1, [1,2,3].indexOf(5)); assert.equal(-1, [1,2,3].indexOf(0)); }); }); }); describe('Array', function(){ describe('#indexOf()', function(){ it('should return the index when the value is present', function(){ assert.equal(1, [1,2,3].indexOf(2)); assert.equal(0, [1,2,3].indexOf(1)); assert.equal(2, [1,2,3].indexOf(3)); }); }); }); 如果我在控制台中键入mocha,testing会起作用,但IDE会在“describe”和“it”的行中显示警告,因为它说variables没有被声明(“未声明的variables”)。 我想知道我应该怎么做这些testing来避免这些警告。 […]

模拟node.js模块

可以说我有这种情况(架构) layer1 -> layer2 -> layer3 层只是正常的node.js模块(有一些导出的function) Layer1需要layer2并调用其function,而layer2需要layer3并调用其function。 我想testinglayer1中的函数,但也模拟layer3(我在layer1中的函数调用传播到layer3,这个我想模拟)。 做这个的最好方式是什么? 我已经看过这个模块: https : //github.com/thlorenz/proxyquire,但我不认为它支持嘲笑当事情是深入2级或更多级别,如我的例子。 感谢您的任何build议!

如何在使用Google Maps for JS的代码上编写unit testing

我正在写一些JavaScript模块,旨在在浏览器(客户端,而不是服务器端)运行。 该模块正在使用Google Maps JavaScript API 。 我想通过unit testing来覆盖我的代码。 我也希望我的testing是孤立的。 我发现了几个 类似vcr的JS 库,用于录制和嘲笑Google Maps Api生成的HTTP请求。 但所有这些都是为了Node.JS(因为PhantomJS不支持使用fs模块 )。 此外Node.JS比PhantomJS拥有更丰富和可读的回溯。 所以我想知道如何使用基于Node.JS的testing运行器将Google Maps JavaScript API包括到我的testing中,以及如何为我的代码编写testing? PS我不会坚持一些JSunit testing库。 它可以是茉莉花,QUnit或任何其他。 PPS没必要应该是Node.JS亚军。 如果还有其他select,那完全没问题! PPPS我的目标是避免以下事情: 避免依赖互联网连接和相应的testing延迟 以避免由于更改Google服务器上的某些GEO数据而导致testing失败。 例如:如果我使用方向,我不在乎它是2000米还是2001米,我只是想知道,我从Google得到一些适当的数据,并用它进行一些计算。 PPPPS感谢@MichaelGeary的回答,我们知道Google只保存3个版本的API。 但我不是只关注Google地图,因为它受欢迎程度,所以我select了这个问题。我有同样的问题适用于像Yandex.Maps , Leaflet(与openstreet) , Bing等任何其他地图api。大多数他们不删除旧的API,所以我可以修复版本,并依靠不改变内部的API和HTTP请求。 我也想避免模拟地狱,因为我的代码是相当复杂的,并使用了很多不同types的地理对象。 所以嘲笑所有这些并不容易,然后支持该代码。 它看起来像难以承受的事情。 我的想法是修复API的版本一段时间(在谷歌案件不长的时间),并依靠内部HTTP请求格式的持久性。 并不时删除所有logging的数据,以确保在现实世界中一切都还可以。 我希望自己成为一个能够控制自己testing的人。 我不想让Google随意打破我的testing。

复制Node.js`assert`断言spec`expect`

与使用自定义的开发人员友好检查的非断言代码相反, class Some { constructor(arg) { if (Array.isArray(arg) && arg[0] === 'foo') this.foobar = arg.concat('bar').join(''); else console.error('Bad Some constructor arg'); } } 目前testing的代码与Node assert断言具有合理有意义的message参数: class Some { constructor(arg) { assert.deepEqual(arg, ['foo'], 'Some constructor arg'); this.foobar = arg.concat('bar').join(''); } } 这个说法是有的 保持代码平坦和可读 对调用堆栈的不正确使用提供有意义的反馈 防止函数执行,不会进一步传播错误 抛出错误并将error handling留给调用者 目前的规格可能如下所示: it('…', () => { let some = new Some(['foo']); […]

用SailsJS和Superagent运行摩卡testing

我目前正在使用SailsJS编写一个应用程序。 到目前为止所做的工作都是按照“手动”进行testing,但是在使用Mocha进行testing时没有发生。 我试图按照SailsJStesting指南 ,用npm调用testing: […] "scripts": { "start": "node app.js", "debug": "node debug app.js", "test": "mocha test/bootstrap.test.js test/unit/**/*.test.js" }, […] 我的testing目录结构如下: test ├── bootstrap.test.js ├── mocha.opts └── unit └── controllers └── UserController.test.js boostrap.test.js: var Sails = require('sails'); var sails; before(function(done) { Sails.lift(function(err, server) { sails = server; if (err) return done(err); done(err, sails); }); }); […]