Tag: 超级特技

testingREST API – req.body undefined(Node.js / Express / Mocha / Supertest)

我正在尝试testingNode中构build的REST API。 我用Postman手工testing了这个API,没有问题,但是我用Mocha / Chai / Supertest编写testing时遇到了麻烦。 当我尝试testing发送到路由时,请求主体是未定义的。 在我迄今为止的研究中,似乎无法find与我正在做的和其他人有任何有意义的区别,但由于某些原因,我尝试发送的数据没有通过。 以下是路线的处理方式: router.route('/media') .post(RouteController.postMedia); RouteController.postMedia = function(req, res) { var media = new Media(); console.log(req.body); media.title = req.body.title; media.type = req.body.type; media.tags = req.body.tags; media.pubdate = req.body.pubdate; media.editdate = req.body.editdate; media.filename = req.body.filename; media.extension = req.body.extension; media.description = req.body.description; media.save(function(err) { if (err) res.send(err); res.json({ message: 'File […]

与摩卡失败的快速testing

我有一个Express服务器,我试图用SuperTesttesting。 下面的testing没有通过,我不知道为什么。 我看到响应的状态是200(在res.status.should.equal(200)放置了断点)。 为什么这个testing仍然被标记为我的摩卡失败? it('Should test invoke user method', function (done) { supertest(app) .post('/save/test1/test2/test3') .expect(200) .end(function (err, res) { res.status.should.equal(200); done(); }); });

为什么我在testing中得到401回应?

我正在尝试在Node / Express / Mongoose后端testing一个具有身份validation的路由。 这是testing文件 var should = require('should'); var _ = require('lodash'); var async = require('async'); var app = require('../../../../app'); var request = require('supertest'); var mongoose = require('mongoose'); var User = mongoose.model('User'); var Firm = mongoose.model('Firm'); var firm, user, userPassword, createdFirm, loggedInUser; describe('GET /api/firms', function(){ beforeEach(function (done) { firm = new Firm({ company: […]

节点:Mocha TDD正在发送数组

以下是我需要在请求中发送的以下正文格式: [{ "user_id": "861", "username": "userA", "friend_id": "1270" }, { "user_id": "861", "username": "userB", "friend_id": "1270" }] 我创build了以下testing: describe('POST /friends/add', () => { it('should return an array of Friend Objects ', (done) => { request(app) .post('/friend/add') .set('auth', token) .send([ { "user_id": authUserId, "username": filteredUsers[0].username, "friend_id": filteredUsers[0].id }, { "user_id": authUserId, "username": filteredUsers[2].username, "friend_id": filteredUsers[2].id }, […]

Express JS与Supertest和模拟数据库的集成testing

是否可以使用supertesttestingExpress JS REST API,但用模拟数据库对象replace实际的数据库连接? 我有unit testing覆盖数据库模型和应用程序的其他部分,以及API端点的functiontesting,使得实际的数据库连接,但我有一个奇怪的要求,创build像functiontesting,但使用模拟数据库连接的集成testing。 示例端点控制器如下所示: var model = require('../../../lib/models/list'); module.exports = { index: function(req, res) { var data = { key: 'domains', table: 'demo.events'}; var dataModel = new model(data); dataModel.query().then(function(results) { res.respond({data: results}, 200); }).fail(function(err) { console.log(err); res.respond({message: 'there was an error retrieving data'}, 500); }); } }; 而URI的索引是 var express = require('express'), […]

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); […]

Supertest,我可以创build一个替代的请求与默认设置一些标头?

我正在用Mocha的Supertest来testing用Node JS开发的API。 我想在API上做很多不同的testing。 几乎所有的人都必须重新设置Authorization和Content-Type头(因为API需要这个testing)。 it('Creation without an email address should fail and return error code 50040', function(done) { request .post('/mpl/entities') .set('Authorization', 'Token 1234567890') //set header for this test .set('Content-Type', 'application/json') //set header for this test .send({ firstname: "test" }) .expect('Content-Type', /json/) .expect(500) .expect(anErrorCode('50040')) .end(done); }); it('Creation with a duplicate email address should fail and return […]

testing在节点中使用mocha / supertestredirect的请求

我似乎无法得到以下的集成testing,通过使用摩卡 , supertest , 应该 (和coffeescript)的快速项目。 考试 should = require('should') request = require('supertest') app = require('../../app') describe 'authentication', -> describe 'POST /sessions', -> describe 'success', (done) -> it 'displays a flash', (done) -> request(app) .post('/sessions') .type('form') .field('user', 'username') .field('password', 'password') .end (err, res) -> res.text.should.include('logged in') done() 相关的应用程序代码 app.post '/sessions', (req, res) -> req.flash 'info', […]