Tag: 摩卡

variables作用域如何在Mochatesting框架内工作?

我是一个JavaScript,node.js,摩卡等所有东西的相对新手。 在我的代码中,我有一个单元对象有一个disable() ,设置禁用属性为true和isDisabled() ,返回禁用的属性。 它也有一个方法nextTurnReset() ,在下一回合开始时重置单位。 我写了一个testing套件来testing这种行为。 我首先禁用对象,然后尝试testing以查看它是否被禁用。 但是,我的第一个testing中的单元variables(在传递给Mocha的it()方法的匿名函数中it()处于非禁用状态,正如我在节点的debugging器中观察到的那样。 describe('#disable()', function() { var unit = tests.newUnit(); unit.disable(); debugger; it('disabled off turn?', function() { debugger; (unit.isDisabled()).should.be.exactly(true); }); unit.nextTurnReset(); it('disabled on next turn?', function() { (unit.isDisabled()).should.be.exactly(true); }); unit.nextTurnReset(); it('disabled on 2nd turn?', function() { (unit.isDisabled()).should.be.exactly(false); }); }); 为logging,前两个testing失败,最后一个成功,表明该单位从来没有被禁用。 从使用节点debugging器的repl:在第一个debugger; 声明, unit.disabled == true ,但在第二个debugger; 语句unit.disabled == false […]

如何让摩卡通过testing

我有以下testing: it.only('validation should fail', function(done) { var body = { title: "dffdasfsdfsdafddfsadsa", description: "Postman Description", beginDate: now.add(3, 'd').format(), endDate: now.add(4, 'd').format() } var rules = eventsValidation.eventCreationRules(); var valMessages = eventsValidation.eventCreationMessages(); indicative .validateAll(rules, body, valMessages) .then(function(data) { console.log("SHOULD NOT GET HERE"); should.fail("should not get here"); done(); }) .catch(function(error) { console.log("SHOULD GET HERE"); console.log(error); }); done(); }); […]

在所有使用摩卡的testing之后,在哪里删除数据库并closures连接

我试图找出到哪里放置一个函数来删除数据库,并在所有testing运行后closures连接。 这里是我的嵌套testing: //db.connection.db.dropDatabase(); //db.connection.close(); describe('User', function(){ beforeEach(function(done){ }); after(function(done){ }); describe('#save()', function(){ beforeEach(function(done){ }); it('should have username property', function(done){ user.save(function(err, user){ done(); }); }); // now try a negative test it('should not save if username is not present', function(done){ user.save(function(err, user){ done(); }); }); }); describe('#find()', function(){ beforeEach(function(done){ user.save(function(err, user){ done(); }); }); it('should find […]

npm glob模式不匹配子目录

在我的package.json ,我有一个使用**/*Test.js来匹配文件的脚本块。 当通过npm ,它们不匹配超过一个级别的子目录。 当直接在命令行执行时,它们按预期工作。 任何人都可以解释发生了什么,并提供一个解决方法或解决scheme? package.json { "name": "immutable-ts", "scripts": { "test": "echo mocha dist/**/*Test.js", } } 执行 % npm run test > immutable-ts@0.0.0 test:unit …/immutable-ts > echo mocha dist/**/*Test.js mocha dist/queue/QueueTest.js dist/stack/StackTest.js % echo mocha dist/**/*Test.js mocha dist/queue/QueueTest.js dist/stack/StackTest.js dist/tree/binary/BinaryTreeTest.js % ls dist/**/* dist/collections.js dist/queue/QueueTest.js dist/tree/binary/BinaryTree.js dist/immutable.js.map dist/stack/Stack.js.map dist/tree/binary/BinaryTreeTest.js.map dist/immutable.js dist/stack/Stack.js dist/tree/binary/BinaryTreeTest.js dist/queue/Queue.js.map […]

我如何testing摩卡未被捕获的错误?

我想testing下面的函数按预期执行: function throwNextTick(error) { process.nextTick(function () { throw error; }); } 这是我的尝试: describe("throwNextTick", function () { it("works as expected", function (next) { var error = new Error("boo!"); var recordedError = null; process.once("uncaughtException", function (error) { recordedError = error; }); throwNextTick(error); process.nextTick(function () { recordedError.should.be(error); next(); }); }); }); 但摩卡似乎想保留任何错误本身,并得到它们时,我的testing失败: C:\Users\ddenicola\Programming (Synced)\pubit>mocha test/basicTest.js throwNextTick 0) works […]

Node.js / Express / Mocha / Supertest Rest API – 空请求正文

我到处寻找解决办法。 我唯一发现的是一个没有回答的post。 我很抱歉,如果我忽略了一些东西。 问题是,当我尝试在/createQuestion API中获取POST值时,正文为空/未定义。 我得到这样的错误Cannot read proprety 'question' of undefined来自API Cannot read proprety 'question' of undefined 。 Express API: app.post("/createQuestion", function(req, res) { var questionType = req.body.question.type; var questionText = req.body.question.text; var questionDuringClass = req.body.question.duringClass; // Do a bunch of stuff res.send(response); }); 考试: var should = require('should'); var assert = require('assert'); var request […]

testing下载是否成功与超级

我正在用超级testing我的API端点,它工作的很好,但我不知道如何testing文件下载是否成功。 在我的路线文件中,我已经定义了端点: app.get('/api/attachment/:id/file', attachment.getFile); 和函数getFile()看起来像这样: exports.getFile = function(req, res, next) { Attachment.getById(req.params.id, function(err, att) { […] if (att) { console.log('File found!'); return res.download(att.getPath(), att.name); } 然后,在我的testing文件中,我尝试以下内容: describe('when trying to download file', function() { it('should respond with "200 OK"', function(done) { request(url) .get('/api/attachment/' + attachment._id + '/file'); .expect(200) .end(function(err, res) { if (err) { return done(err); […]

尝试使用MochatestingNode.js服务器进程

Node.js相当新颖 做了一个运行服务器进程并提供文件的应用程序(不使用快速或任何框架),现在我试图unit testing它。 我试图使用摩卡testing…我打算启动我的服务器进程,然后运行请求来testing预期的结果(统计代码,正文内容等) 然而,它不能正常工作,所有的请求无法连接到服务器…我敢肯定,这个问题是因为节点是运行一个进程循环,服务器没有运行“在后台”,而查询运行或者可能服务器还没有运行(开始ASYNC),而请求正在进行? 无论如何,我想知道什么是testing这个正确的方法,我假设要么我需要在后台运行服务器(如分叉进程)和/或者我需要find一种方法来等待服务器进程先“起来”,但不知道如何。 或者至lessbuild议testing这样的服务器进程(与摩卡或其他)。 谢谢。 这里是示例testing代码(从原始问题更新) var server = new Server302('./fixture/'); var instance; describe('Tests', function() { before(function(done) { instance = http.createServer(function(request, response) { console.log(request.url); server.serve(request, response); }).listen(8000); instance.on("listening", function() { console.log("started"); done(); }); }); after(function(done){ instance.close(); console.log("stopped"); done(); }); it("Should fetch test.html", function(done) { console.log("test1"); http.get("http://localhost:8000/", function(res) { res.on('data', function(body) { console.log(body) […]

如何使用mochatesting框架与node.js和sails.js

我想为node.js使用mocha。 我使用的最后一个testing框架是Ruby on Rails的Rspec,所以我试图用相同的方法来完成,但是我得到了巨大的框架和我可以使用的所有库。 我正在跟进官方上手,但并没有解释如何组织testing。 http://visionmedia.github.io/mocha/#installation 现在,我正在阅读,我可以使用以下库: https://github.com/visionmedia/should.js – testing模型实例的东西。 https://github.com/LearnBoost/expect.js – 基于should.js的简约BDD断言工具包 http://chaijs.com/ – 看起来很大,包括应该,期待和另一个lib https://github.com/visionmedia/better-assert – 使用callsite进行自我logging失败消息的更好的c样式断言。 (到目前为止我其实并不了解这个目的,看起来并不比别人好) https://github.com/rjanicek/mocha.js-haxe – 看起来是浏览器端,但摩卡也表示,它正在运行浏览器端。 而且我知道还有更多,这只是我在摩卡官方网站上看到的名单。 对于我所能理解的,看上去chai是用摩卡咖啡的,你怎么看? 而且,到目前为止,我从来没有看到任何东西来帮助我决定在哪里写testing(当然,在/testing/当然),以及如何组织一切。 我也使用伟大的sails.js框架(基于express)和pomelo.js为不同的项目,我需要在这两个框架上使用相同types的testing,所以我正在寻找一个通用的架构和库,我可以同时使用(所以,sails.js没有特定的东西,但可以直接使用任何其他框架) 这是我计划组织testing的方式,你认为这是一个正确的架构吗? 节点的主要问题是有很多的框架,插件,库和我不知道什么是最好的select,node.js是一个巨大的社区真的很大,真的很难概括所有的可能性。 你如何处理你的testing?

茉莉花(摩卡)嵌套“它”testing

我试图testing随后的创build/删除项目(在mongoDB通过mongoose)。 创build是asynchronous的问题,它返回创build的项目在callback函数中的ID,我需要这个ID删除创build的项目,所以我尝试了摩卡(以不同的方式)下面的代码,但它没有工作。 describe('Item Model', function(){ it('should be able to create item', function(done){ var item = new Item({name: {first: "Alex"}); item.save(function(err, data){ it('should be able to deleted created item', function(done){ Item.delete({_id: data.id}, function(err, data){ done(err); }); }); }) }); }); 这种testing可以在摩卡或茉莉花中实施吗?