Tag: supertest

testing后closures服务器和数据库连接

我有与mongo连接的koa服务器,并使用supertest模拟服务器和发送请求,作为testing框架。 const app = new Koa() … export default app.listen(PORT, (err) => { if (err) console.log(err) if (!IS_TEST) { console.log(`Server running on port: ${PORT}`) } }) 在成功完成testing或者服务器连接失败之后,koa服务器连接testing结束多久? testing例子: import supertest from 'supertest' import mongoose from 'mongoose' import server from '../../../app/server' import User from '../../../app/models/user' const r = supertest.agent(server.listen()) afterEach(async (done) => { await mongoose.connection.db.dropDatabase() done() […]

用于testingExpress应用程序的状态交互

我用express写了一个简单的JSON API,我试图用mocha做一些黑盒testing。 通过对API进行testing,需要对不同的用户进行身份validation,因此每个针对特定function的testing都至less由两个请求组成:一个login操作和一个或多个validation请求,用于testing实际function。 我还没有find类似于django.test.client库来模拟HTTP客户端和服务器之间的有状态交互。 Supertest似乎很受欢迎,但与djangotesting客户端相比,它是非常低级的。 这是我将如何写一个简单的authenticationtesting(请原谅我的咖啡标记): it 'should return a 200 OK', (done) -> supertest(server.app) .post('/login') .send("username=xxx&password=pass") .end (err, res) -> res.should.have.status(200) supertest(server.app) .get('/api/users') .set('cookie', res.headers['set-cookie'][0]) .expect(200, done) 这真的是最干净的方式来执行交互? 有什么图书馆可以帮助我解决asynchronous问题(这不是我需要的东西,而是99%的情况下,testing的简单序列化,callback只是混淆)和有状态? 一些会这样的事情: it 'should rock', (done) -> myCoolLibrary [ -> @post '/login', {username: "xxx", password: "pass"}, (err, res) => res.should.have.status 200 @done() , -> @get '/api/users', […]

无法从超级应用程序请求中捕获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) + […]

Supertest中的否定断言

初学者用Javascript进行testing。 我正在使用摩卡,但路易聪明地没有说这个问题是摩卡特有的。 我有一个节点应用程序,有一些匿名用户可见的页面,有些则不应该能够看到你是否没有login。所以,作为一个非常简单的开始, describe('User Access', function(){ it('should allow anyone to access the help desk (about page)', function(done){ request(host) .get('/') .expect(200, done); }), it('should allow anyone to access the contact page', function(done){ request(host) .get('/contact') .expect(200, done); }), //initially we were expecting 404, we need anything BUT 200. it('should NOT allow anonymous user to access the Training […]

如何使用express,sequelize,mocha和supertests进行回滚unit testing

我试图做一个testing插入数据到数据库,testing一个请求与超级,然后回滚与假数据。 有人知道这种方式是正确的吗? 如果不是,请您回答并告诉我哪个方法正确? 提前致谢。 … var app = express(); var request = require('supertest'); var assert = require("assert"); var db = require('../../models'); var mysql = require('mysql'); describe('[Test controller – send_confirmation_email.js]', function () { describe('POST /crowdfunding/sendConfirmationEmail', function () { it('Second post test with data', function (done) { db.sequelize.transaction(function (t) { var cf = db.Crowdfunding.build({ money_raised: 80, project_id: […]

Sails.js和Mocha:使用supertest创build一个新的模型

我目前正在为我的Sails应用程序设置testing基础架构,并且一直在顺利进行,直到我尝试使用supertesttesting来testingAPI请求。 我试图testing一些我的控制器方法(我实现,而不是使用默认蓝图路线),但似乎API请求甚至没有经过。 我认为这是因为我可以运行npm test ,这段代码运行良好,但是如果我将POSTpath更改为/datamodel/create5 ,其中create5()不作为控制器方法存在,它仍然运行正常。在这两种情况下,都不会创buildDataModel模型。 我已经在下面包含了一些代码。 这是我的代码看起来像: var request = require('supertest'); var assert = require('assert'); var async = require('async'); var stubs = require('../stubs.js'); describe('DataModel', function() { var testDataModel; var dataModelParams = stubs.dataModelStub(); // simply returns a JSON dictionary describe('#create()', function() { describe('data model import', function() { it('should import a new data model.', function (done) […]

NodeJS和supertest:用npm命令开始testing

我用supertest 。 我想在testing目录中启动所有testing。 我的/tests/tests.js文件: var request = require('supertest'); var app = require('express.io')(); //add good url with http:// and redirection request(app) .post('/add/') .expect(201) .send({url: 'http://www.google.fr'}) .end(function(err, res){ console.log(res.body.url); if (err) throw err; request(app) .get('/redirect/' + res.body.url.generatedid) .expect(302) .end(function(err, res){ if (err) throw err; }); }); //add good url with www. and redirection request(app) .post('/add/') .expect(201) .send({url: […]

保持Supertest直到API初始化

我使用一个使用MongoDB的快速API的Supertest进行摩卡testing。 MongoDB正在运行,但是我目前正在使用Supertest来使用Express API,而不是单独启动它(我更喜欢这种方法): var request = require( 'supertest' ); var chai = require( 'chai' ); var api = require( '../../server/api.js' ); chai.should(); describe( "/api/lists", function() { it( "should be loaded", function() { api.should.exist; } ); it( "should respond with status 200 ", function( done ) { request( api ) .get( '/api/lists' ) .expect( 200, done […]

摩卡超时无论如何都超过了

我目前正在摩卡书写我的nodejs应用程序的testing。 我的API调用要求我login,所以我想创build一个包装testing套件,创build一个testing用户,然后调用实际的testing套件。 下面是代码的样子: var request = require('supertest'); var config = require('../config/config'); var AdminUser = require('../models/Authmodel'); function configureAuth(test_suite) { var url = "localhost:" + config.port; var email = "test@test.com"; var password = "test_password"; var admin; var token; describe("Signup User", function() { it("should signup new user", function(done) { request(url) .post('/auth/signup') .send({ email: email, password: password }) .expect(200) […]

根据URLpath创build代理

我需要根据来自浏览器的一些URL创build代理,因为我相当新的这个主题林不知道如何testing它:(我需要一些方法来testing它,看看这实际上是工作从这个博客使用下面的代码 http://blog.nodejitsu.com/node-http-proxy-1dot0/ var httpProxy = require('http-proxy') var proxy = httpProxy.createProxy(); var options = { 'foo.com': 'http://website.com:8001', 'bar.com': 'http://website2.com:8002' } require('http').createServer(function(req, res) { proxy.web(req, res, { target: options[req.headers.host] }); }).listen(8000); 我需要的是当你把浏览器localhost:8000你将路由(代理)到新的服务器与不同的path,如选项中所述。