Tag: 摩卡

如何在node.js + Express.js + mongodb应用程序上asynchronous启动摩卡testing

问题似乎很微不足道,所以我真的很尴尬,我不能自己解决它,但我做了很多search谷歌和stackoverflow,没有任何帮助我呢。 我正在用node.js开发RESTful服务器,我想testing它的API。 服务器asynchronous初始化(它需要连接到数据库,从另一台服务器等请求令牌)。 所以我的server.js文件中的代码如下所示: var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var MongoClient = require('mongodb').MongoClient; var bunyan = require('bunyan'); // logger var routes = require('./routes/folrouter.js'); var TokenGetter = require('./utils/tokengetter'); MongoClient.connect('mongodb://localhost:27017/fol', function (err, db) { if (err) throw err; global.db = db; global.log = bunyan.createLogger({ name : "folserver", src : true, […]

如何使用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: […]

在Node中,如何检查一个特定的对象是否是一个ChildProcess的实例

我试图检查(在unit testing)如果一个特定的对象是一个ChildProcess,但我似乎无法得到该类的引用(这是https://github.com/joyent/node/blob /7c0419730b237dbfa0ec4e6fb33a99ff01825a8f/lib/child_process.js ) 我想要做的是类似的东西 selenium = require('selenium-standalone') spawnOptions = { stdio: 'pipe' } seleniumArgs = ['-Dwebdriver.chrome.driver=./node_modules/nodewebkit/nodewebkit/chromedriver'] @server = selenium(spawnOptions, seleniumArgs) expect(server).to.be.an.instanceof(ChildProcess) 但我似乎并不知道在哪里得到对ChildProcess的引用 更新:基于提供的答案,我现在正在testing使用: it 'start()', (done)-> selenium.start.assert_Is_Function() selenium.start -> selenium.server.assert_Is_Not_Null() selenium.server.constructor.assert_Is_Function() selenium.server.constructor.name.assert_Is('ChildProcess') done()

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只通过,如果没有事先testing

我正在testing一个MEAN应用程序。 每个testing只是确保我得到正确的回应代码。 在我的路由器中,他们使用res.send()成功返回json。 这里给出的第二个testing只有当第一个被注释掉时才会通过。 如果我把第一个testing评论出来,第一个testing通过,但第二个testing通过。 这种行为对于这两个testing不是唯一的。 在“接受经济长期”之前,还有另外一个考验。 如果我评论一下,接受经济和长期的工作。 如果我离开它,接受经纬度。 我需要做些什么才能让这些asynchronoustesting通过? 我曾尝试将超时设置为60秒,但这也不起作用。 var assert = require('assert'); var server = require('../bin/www'); var request = require('supertest'); request = request('http://localhost:3000'); describe('POST service request', function(){ this.timeout(5000); var postRequest = request.post('/requests.json').type('form'); … (other commented out tests) … // it('accepts lat and long', function (done){ // postRequest.send({ // service_code: 2000, // long: […]

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

摩卡testing花费太长的时间来执行

所以我正在一个express.js应用程序,我有一个mongoose模型用户。 我写了一个testing文件(使用Mocha)来testingsave()函数,但是我所有的testing都花费很长时间来执行并最终超时。 这是我得到的错误: Testing – Server – User – Model Testing save() 1) should be able to save without problems 2) should fail to save an exisitng user again 3) should should an error when try to save with empty email 4) should give an error when try to save with empty password 5) "after […]

在Heroku NodeJS服务器上运行MochaJStesting

我为我的ExpressJS / NodeJS API提供了广泛的MochaJStesting套件。 testing套件包括创build对象和从数据库中删除相同的对象。 目前,我所有的回归testing都通过了我的开发环境。 我们希望能够在分段环境中运行相同的testing,这些分段环境具有在部署新代码之后迁移和未创build的数据对象。 我们如何从服务器运行我们的摩卡testing? 目前,我使用以下命令在本地运行我的testing: foreman run node node_modules/mocha/bin/mocha 提前感谢任何帮助!

如何在Node.js中模拟一个glob调用

我正在使用Mocha,Chai和Sinon JS为我的Node.js应用程序编写unit testing。 这是我想testing的模块: var glob = require('glob'); var pluginInstaller = require('./pluginInstaller'); module.exports = function(app, callback) { 'use strict'; if (!app) { return callback(new Error('App is not defined')); } glob('./plugins/**/plugin.js', function(err, files) { if (err) { return callback(err); } pluginInstaller(files, callback, app); }); }; 我有一个testing的情况下,当没有应用程序使用.to.throw(Error) 。 但是我不知道如何模拟glob调用。 特别是我想告诉我的testing方法,glob-call返回什么,然后检查,如果pluginInstaller被调用,使用sinon.spy 。 这是我迄今为止的testing: var expect = require('chai').expect, pluginLoader […]