Tag: 达摩卡

在Nodejs中实现unit testing – 意外的失败

无法执行unit testing 问题是testing没有通过,尽pipe当我执行testing时请求和响应数据被添加到数据库。 请提出一些方法来通过这个testing。 预期的状态码200没有在这个testing中发生。 集成testing正在对我的应用程序工作,但我无法debugging此unit testing错误。 //testfile var db = require('../mongodb'), mongoose = require('mongoose'), posts = require('../api/addTodo'), should = require('should'), testUtils = require('./utils'); describe("Add Api", function () { var dummyPost, id; before(function (done) { mongoose.connect('mongodb://localhost:27017/todosdb_test', function() { console.log('Connected To:'+'mongodb://localhost:27017/todosdb_test'); done(); }); dummyPost = new db.Todos({ 'admin': 'Dumm_admin', 'text': 'Dummy', 'completed': true }); dummyPost.save(function (err, […]

req.checkQuery(''); &req.checkParams(''); validation – >错误句柄(node.js)

我正在尝试使用express-validator的 req.checkQuery(''); req.checkParams(''); 简单地检查/validation,并确保我的查询参数是有效的。 通过这个摩卡错误: error handling handles invalid query params: Error: expected 422 "Unprocessable Entity", got 200 "OK" 我在这方面尝试过; 我没有find这两个具体的文件。 任何使用checkQuery和checkParams指针? 我可以通过这些查询所有查询和参数吗? app.get('*', function(req, res) { req.checkQuery(''); req.checkParams(''); }); 产生: '错误:预期422“不可处理的实体”,得到200“OK”“node.js /快递应用程序

摩卡testing:“TypeError:undefined不是一个函数”在Test.serverAddress

我有以下testing: describe('Testing the GET methods', function() { it('Should be able to get the list of articles', function(done) { // Create a SuperTest request request(app).get('/api/articles/') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200) .end(function(err, res) { res.body.should.be.an.Array.and.have.lengthOf(1); res.body[0].should.have.property('title', article.title); res.body[0].should.have.property('content', article.content); done(); }); }); it('Should be able to get the specific article', function(done) { // Create a SuperTest request […]

正确的方式来testing快速应用程序API?

我已经find了一个办法,但我的直觉告诉我应该有一些更习惯性的做法。 基本上我不喜欢的是,我必须要求在testing套件中的快速应用程序,这让我想知道是否有一个种族条件正在进行。 另外,我想知道如果我在几个这样的文件中运行几个testing套件会发生什么。 任何人都知道更清洁的解决 我简化的应用程序如下所示: app.js app = module.exports = express() … http.createServer(app).listen(app.get('port'), function(){ console.log('app listening'); }); test.js var request = require('superagent'); var assert = require('assert'); var app = require('../app'); var port = app.get('port'); var rootUrl = 'localhost:'+port; describe('API tests', function(){ describe('/ (root url)', function(){ it('should return a 200 statuscode', function(done){ request.get(rootUrl).end(function(res){ assert.equal(200, res.status); done(); […]

我将如何testing用mocha / chai / superagent发送路线?

it('sends login page if we\'re logged out at /', function (done) { superagent.get('http://localhost:4000/').end(function(err, res){ if(err) {return done(err)} expect(res).to.have.property('status', 200); //how would i do something like this? expect(res.viewRendered).to.be.equal('index.ejs') done(); }); }); 我是新来的testing,我讨厌它..很多。 我正在努力学习基础知识,这是我所经历的最令人沮丧的学习曲线。 我一直在查找文件几个小时,仍然无法弄清楚如何检查哪条路线已被渲染

unit testingExpress Middleware的正确方法

我有一个Express中间件,它被设置为在我的服务器的所有POST请求中检查有效的Content-Type头,这个中间件的代码如下: import * as STRINGS from "../Common/strings"; function ContentTypeValidator(req, res, next) { let contentHeader = req.get("content-type"); if(!contentHeader) { res.status(400).send(STRINGS.ERROR_CONTENT_TYPE_MISSING); } else { if(contentHeader.toLowerCase() !== "application/json") { res.status(415).send(STRINGS.ERROR_CONTENT_TYPE_UNSUPPORTED); } else { next(); } } } export default ContentTypeValidator; 我为我的TDD使用了mocha , chai和node-mocks-http ,当next()不会被调用时,我的问题围绕着testing, res.send()将为我处理这个请求的结束。 it("Should return 200 for valid Content-Type header", (done) => { req = nodeMocks.createRequest({ […]

如何检测我的express(4.0)node.js应用程序是否使用特定的中间件?

为了进行unit testing,我希望能够解决一些中间件是否在使用中。 例如 app.js var express = require('express'); var morgan = require('morgan'); module.exports = function(options) { var app = express(); if (options.logRequests) { app.use(morgan('dev')); } return app; } app.test.js (通过摩卡运行) describe('app', function(){ it('should log requests when initialized with the `log requests` option', function() { var app = require('./app)({ logRequests: true }); // Something to establish […]

Node.js – 使用摩卡testing快速应用时出现“错误:连接ECONNREFUSED”

首先,我不听80或8080端口,我正在听1337端口。 我使用express创build了一个简单的HTTP服务器。 这里是启动服务器的app.js脚本。 require('./lib/server').listen(1337) 服务器脚本位于lib/server.js文件中,因为在testing脚本中也会使用相同的脚本。 var http = require('http') , express = require('express') , app = express() app.get('/john', function(req, res){ res.send('Hello John!') }) app.get('/sam', function(req, res){ res.send('Hello Sam!') }) module.exports = http.createServer(app) 最后是test/test.js : var server = require('../lib/server') , http = require('http') , assert = require('assert') describe('Hello world', function(){ before(function(done){ server.listen(1337).on('listening', done) }) after(function(){ server.close() […]