Tag: 摩卡

我怎样才能让我的node.jsunit testing识别node_modules?

我有以下代码: var foo = require('foo'); /* module.exports = function(n){ return 2 * n} */ describe('basic', function(){ describe('body', function(done){ foo(2).should.equal(4); }); }); 这是在[project_root]/tests/foo_test.js 。 foo.js位于[project_root]/node_modules/foo.js 。 但是,当我从[project_root]运行mocha (visionmedia的testing套件) foo_test.js无法加载foo.js proces.cwd()显示[project]作为进程的根。 为什么不在我的testing环境中加载foo.js ?

unit testing与Mocha OAuth JS

我正在开发基于JS的项目,运行GAE,部分代码使用来自Facebook,Twitter或Google的OAuth获取用户的头像。 我试图在摩卡写testing,以testing这个,但我遇到了一些问题。 当我在前端testing代码时,代码就起作用了,我设想它的工作方式是使用ZombieJS在GAE的dev_appserver.py上运行应用程序,激发OAuth函数,填充相应的auth东西,然后完成通过返回图片url进行testing。 不过,我得到的第一个障碍是NodeJS的服务器似乎不允许GAE的服务器运行在同一个IP地址上。 例如: exec 'dev_appserver.py .', -> console.log arguments 这将返回错误“地址已被使用”。 除了在不同的机器上运行,我怎么能解决这个问题? 是否有可能告诉NodeJS不保留整个IP和只是一个端口? 我在8080上运行GAE,当它没有被NodeJS调用时,它工作正常。 第二个问题是ZombieJS。 我试图找出一种方法,我可以听到新窗口打开时,本质上, tail浏览器的控制台。 我已经开始对Google小组进行两次讨论,但还没有人回复(https://groups.google.com/forum/?hl=en#!topic/zombie-js/cJklyMbwxRE和https://groups.google .com / forum /?hl = en#!topic / zombie-js / tOhk_lZv5eA ) 虽然后者并不重要 ,但我可以find解决办法(我希望),前者是主要问题,所以我非常感谢如何解决这个地址冲突的任何方向。 这是我的NodeJS脚本: exec = ( require 'child_process' ).exec fs = require 'fs' should = require 'should' yaml = require 'yaml' Zombie = require 'zombie' […]

在另一个文件中包含文件

我正在尝试使用摩卡进行一些testing驱动开发。 在进入软件工程之初,我遇到了一个问题,包括test.js中的Graph.js: 项目/图表/ Graph.js: function Graph(){ this.nodes = {}; this.id = 0; } Graph.prototype.newId = function(){ return this.id++; }; Graph.prototype.addNode = function(data){ var node = new Node(graph, this.newId()); node.data = data || {}; this.nodes[node.id] = node; return edge.id; }; Graph.prototype.removeNode = function(node){ for(var key in this.nodes){ for(var key2 in this.nodes[key].to){ if(this.nodes[key].edges.to[key2] == this){ delete this.nodes[key].edges.to[key2]; […]

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

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

不pipetesting失败,摩卡都会运行吗?

就像标题所说的那样。 即使testing失败,在Mocha工作before / after / before / after afterEach挂钩吗?

用摩卡testing时,“rest()多次调用”restler错误

当我testing一个restler调用时,我得到一个done()多次错误。 如果我只让restlers调用onece,那不会出错,但是如果我在同一个testing运行中调用同一个方法两次,那么这个错误是错误的。 这里是testing的简化版本 myModule = require 'myModule' describe 'foo', -> describe 'bar', -> it "should hi", (done) -> myModule.hi done it "should hi again", (done) -> myModule.hi done 这里是myModule rest = require 'restler' exports.hi = (done) -> rest.get('http://google.com' ).on "complete", (data, response) -> console.log 'getting called once' done null, data 我应该担心与restler的asynchronous多个请求? 什么是造成这个错误? 控制台日志输出,当我添加 ◦ should […]

如何testing自己的MongoDB包装

我已经编写了自己的用于Node.js的瘦蒙哥包装,以消除代码重复。 但是,我在使用Mocha和Should运行asynchronousunit testing时遇到问题。 会发生什么情况是,由MongoDB驱动程序而不是Mocha捕获的应用程序库引发的任何exception。 也就是说,Mocha既不会捕获错误,也不会调用done()函数。 因此,摩卡打印出一个错误Error: timeout of 2000ms exceeded 。 包装模块db.js片段 var mongodb = require('mongodb').MongoClient; exports.getCollection = function(name, callback) { mongodb.connect(dbConfig.dbURI, {auto_reconnect: true}, function(err, db) { if (err) return callback(err, null); db.collection(name, {strict: true}, callback); }); }; 摩卡test.js var should = require('should'); var db = require('./db.js'); describe('Collections', function() { it.only('should retrieve user collection', function(done) […]

摩卡fs.existsSync总是失败

我正试图检查一个文件是否存在于我的摩卡testing中。 我知道这个文件存在于testing文件夹中(为了简单起见,我把它放在那里,而我试着让它工作)。 但是不pipe我做什么,fs.existsSync总是失败。 Logger.startup()创build文件。 Logger.getFilename()返回一个类似于5-17-30-2013.log的值。 我是摩卡新手,所以如果我犯了一个经典的错误,我不知道。 据我所知,我正在同步使用摩卡。 感谢所有的帮助。 这是我的摩卡testing脚本: var logger = require('../logger'); var fs = require('fs'); describe("Logger", function () { it("Creates a file", function () { logger.startup(); console.log(logger.getFilename()); if (!fs.existsSync(logger.getFilename())) { throw ("Logging File Does Not Exist"); } }) })

更好的方式来dynamic改变testing的代码覆盖需要声明?

我有一个node.js库,我已经设置了与摩卡和柴的 BDD。 我还添加了伊斯坦布尔的代码覆盖率。 一切都通过咕噜任务运行。 我挣扎了一会儿,终于在基于这个存储库上的设置之后,终于find了工作: https : //github.com/morkai/h5.buffers 。 它的工作,但我不喜欢的实施。 grunt-istanbul插件处理文件,设置一个环境variables,testing使用环境variables来查找仪表testing的位置。 这是我不喜欢的最后一部分。 //Gruntfile.js // … env: { "default": { LIB_FOR_TESTS_DIR: srcLibForTestsDir }, coverage: { LIB_FOR_TESTS_DIR: lcovLibForTestsDir } } // … 然后在testing文件中,代码是: var FooClient = require((process.env.LIB_FOR_TESTS_DIR || '../lib') + '/foo-client.js'); 在运行testing文件之前,我还在testing中使用了一个帮助程序来进行初始化。 所以我有两个想法。 使用testing助手提取文件并隐藏其中的详细信息: var foo = require('./test-helper.js').foo; var FooClient = foo.FooClient; var ThatOtherThing = foo.ThatOtherThing; 使用testing跑步者? […]

无法从超级应用程序请求中捕获exception

代码示例如下所示: https : //gist.github.com/sebinsua/8118001 (有三次失败,两次失败,我希望有四次失败,一次成功。) 如果从正常函数抛出,可以用摩卡来捕获AssertionErrors,但是一旦超级包装的应用程序调用函数,我就不能检测到它们。 这是烦人的,因为我想能够注入某些断言到我的快速应用程序,然后我可以testing。 (我正在testing一个有我想testing的副作用的中间件,如果你能告诉我如何模拟请求和响应对象,也可以解决我的问题。) 因为我能够进入间谍,我知道这与国家无法进入无关。 不过,我注意到node.js / express.js / supertest自动将未捕获的exception转换为响应中的错误消息。 也许这就是阻止他们被摩卡testing抓住的原因? 编辑 我刚刚testing了下面的代码,看看这是否是一个普通的http.createServer()问题。 事实并非如此。 这意味着连接,express.js,超级或超级等级都会发生。 (这些代码片段只能工作一半的时间btw …) var http = require("http"), assert = require('assert'); describe('Really crazy test of assertions', function () { it("cannot create a server which throws an assertion error", function (done) { var port = Math.floor(Math.random() * 9999) + […]