Tag: supertest

为什么使用supertest代替expressJS的unit testing?

在search如何testing我的node.js软件时,我遇到了许多build议使用Supertest的答案。 在使用Supertest之前,我正在testing我的应用程序: app.get('/', base_road); function base_road(req, res) { return res.json("Hello world"); } it("base road", function() { base_road(mocked_req, {json: function(data) { assert.isEqual(data, "Hello world"); }}); }); 我对这种testing方式并不满意,因为嘲笑可能会变得很难阅读,但是我觉得如果使用超types,我会比unit testing更接近e2e。 我应该使用supertest做unit testing吗? 我的实际testing方式是否正确?

Node JS – Supertest – 发送响应时获取ECONNRESET

我们想用supertest来testing我们的节点服务器,但是我们面临着现在的问题。 为了综合这个问题,下面的代码: var express = require("express"); var request = require("supertest") var app = express(); app.post('/test_multipart', function(req, res, next){ res.status(200).send("ok") }) var supertest = request(app); function test(){ supertest .post('/test_multipart') .field("pseudo", "test") .attach('pdf_file', 'test.pdf') .end(function(err, res){ console.log(err) console.log(res) }) } test(); 引发以下控制台日志: { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read', response: undefined } undefined 所以,这意味着资源在我们发送它的时刻到超时正在接收的时刻之间变得不确定。 […]

Supertest期望不正确地声明状态码

我有一个像这样的testing: it('should fail to get deleted customer', function(done) { request(app) .get('/customers/'+newCustomerId) .set('Authorization', 'Bearer ' + token) .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(404, done) }); 我已经阅读了这里的文档: https://github.com/visionmedia/supertest 它说: 注意如何直接传递给任何.expect()调用 不工作的代码行是.expect(404, done)如果我改变这个.expect(200, done)那么testing不会失败。 但是,如果我添加这样的结尾: it('should fail to get deleted customer', function(done) { request(app) .get('/customers/'+newCustomerId) .set('Authorization', 'Bearer ' + token) .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200) .end(function(err, res) { if […]

Supertest在黄瓜延迟

我在黄瓜步骤文件中使用supertest向Docker中运行的API发出请求。 另外,我正在使用带有示例的场景大纲,以便请求被执行4次。 编辑:我发现,在黄瓜的第一个请求,我在我的docker日志中得到这个错误:“未能build立9P连接:抓住底层stream的EOF”。 但它只发生一次,从来没有从浏览器或邮递员。 given.js: const request = require('supertest'); module.exports = function givens() { this.Given(/^then system is active$/, function (callback) { this.request = request.agent('http://localhost:8787'); callback(); }); } 然后: module.exports = function whens() { this.When(/^the client requests a list of questions with the parameters of "([^"]*)" and "([^"]*)" and "([^"]*)" and "([^"]*)" and "([^"]*)" and "([^"]*)"$/, […]

Node.js发布请求testingsupertest,express,mocha返回undefined

我曾在网站上看过类似的问题,但我不明白它们如何适用于我的问题: const request = require('supertest'); var express = require('express'); const mocha = require('mocha'); var bodyParser = require('body-parser') var app = express() app.use(bodyParser()); // parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: false })) // parse application/json app.use(bodyParser.json()) describe("Post valid ID", function(){ it("returns expected data", function(done){ var goodID = ({id: '100', name: 'Edith'}); request("http://localhost:8000") .post("/users") .set('Accept', 'application/json') .type("json") .expect('Content-Type', 'application/json; […]

未处理的Promise拒绝警告:TypeError:第一个参数必须是string或缓冲区

问题似乎是重复的,但我一直在努力解决这个从最近3个小时。 基本上我使用supertest & mocha来testing我的API。 我无法理解哪个承诺没有得到解决。 app.post('/todos', (req, res) => { var todo = new Todo({ text : req.body.text }); todo.save().then( (doc) => { res.status(200).send(doc) }, (e) => { res.status(400).end(e); }); }); 以下是我写的testing: const expect = require('expect'); const request = require('supertest'); var {app} = require('./../server'); var {Todo} = require('./../models/todo'); // Setup database beforeEach((done) => { Todo.remove({}) […]

使用SailsJS / Node,Mocha,Supertest进行authentication的会话存储

我有一个SailsJS设置,并使用Mocha / Supertest / Superagent组合来运行unit testing。 我search了四周,阅读supertest,现在它如何扩展superagent.agent('url')以存储会话和cookie。 我知道我/运动员/login和/运动员/当前路线正在工作,因为我可以使用邮差testing他们,他们返回正确的值。 然而,在testing时,我在login时获得了200个状态,但是404 /运动员/当前状态 这是我目前的工作: 1.摩卡testinglogin和validation用户login会话 var should = require('should'), assert = require('assert'), request = require('supertest'); it('/athlete/login should return 200 and athlete on success', function (done){ var athleteStub = AthleteStub(), setPassword = athleteStub.password; Athlete.create(athleteStub, function(err, newAthlete) { var user = request.agent(sails.express.app); user .post('/athlete/login') .send({ 'email': athleteStub.email, 'password': setPassword }) […]

testing一个受保护的API调用,摩卡,supertest,节点js,护照

我有一个保护的API与护照作为中间件: server.get('/api/pipes', auth, api.pipes); 如果用户没有授权,则auth方法返回401。 我有这个testing,看看用户是否login: var postValidLoginCredentials = function(){ return request(url).post('/api/login') .set('Content-Type', 'multipart/form-data') .field('email', 'john.smith@example.com') .field('password', 'example') }; //This pass passes it('should return 200 OK when a user enters a valid user and pass', function(done){ postValidLoginCredentials() .end(function(err, res){ res.should.have.status('200'); done(); }); }); 这是我的受保护的api调用的testing: it('should return 200 OK on GET /api/pipes when user is loggedin', […]

“没有听众检测到”validation错误mongoose和摩卡

我正在尝试为我的快速应用程序的POST函数设置unit testing。 我有以下两个领域简单的mongoose纲要,其中一个是必需的。 当我在validation/必填字段closures的情况下进行摩卡testing时,testing是正确的,但是当所需设置为true时,testing失败,出现以下错误: Uncaught: No listeners detected, throwing. Consider adding an error listener to your connection. ValidationError: Path 'title' is required 有任何想法吗? 正如你所看到的,我肯定已经满足了模式要求,并且包含了标题字段。 这里是我的testing下面:有趣的是,第一个testing似乎工作正常,只有当我试图发布,它会产生错误。 describe('Add one story to:\n\thttp://localhost:XXXX/api/stories', function () { var testStory = new Story( { title: 'Mocha C title', shortTitle: 'Mocha C shortTitle' } ); describe('Story object', function(){ describe('Save story object', function(){ […]

loopback.io错误:发送后无法设置标头。 确认用户

我正在使用摩卡和supertest来创buildfunctiontesting。 以下是testing的副本。 it("should verify user POST /api/User/confirm", function (done) { app.models.Users.findById(userId, function (err, usr) { json('get', '/api/user/confirm?uid=' + userId + '&token=' + usr.verificationToken) .expect(204,done); }); }); 然而这个testing返回: Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (http.js:689:11) at ServerResponse.res.setHeader (/node_modules/loopback-explorer/node_modules/express/node_modules/connect/lib/patch.js:134:22) at ServerResponse.header (/node_modules/loopback/node_modules/express/lib/response.js:700:10) at ServerResponse.send (/node_modules/loopback/node_modules/express/lib/response.js:154:12) at ServerResponse.json (/node_modules/loopback/node_modules/express/lib/response.js:240:15) at ServerResponse.send (/node_modules/loopback/node_modules/express/lib/response.js:142:21) at defaultHandler […]