Tag:

testing将数据保存在数据库中

您好我试图运行摩卡和柴testing来testing我的节点js路由器,它保存在mysql数据库中的用户,然后返回相同的数组。 我现在面临的问题是,当我从本地运行数据库时,我不想将信息保存在数据库中,而当我使用像Travis / Ci这样的连续集成软件时,testing失败,因为没有数据库连接。 我想知道如何testing数据库保存与当前没有实际保存到数据库。 基本上意味着有一个虚拟的虚拟数据库来保存或返回保存。 我读了sinon.js可以帮助,但我不知道如何使用它。 这是我的代码 var expect = require('chai').expect; var faker = require('faker'); const request = require('supertest'); const should = require('should'); const sinon = require('sinon'); const helper = require('../../models/user'); describe('POST /saveUser',()=>{ it('should save a new user',(done)=>{ var fake = request(app) .post('/saveUser') .send({ Owner : faker.random.number(), firstname : faker.name.firstName(), lastname : faker.name.lastName(), email:faker.internet.email(), […]

摩卡柴testing受保护的路线

我有一个Express服务器,我使用passport-local进行身份validation。 我有以下受保护的路线: app.post("/api/test", connect.ensureLoggedIn("/"), (req, res) => { let test = new Test(req.body); test .save() .then(data => { return res.json(data); }) .catch(e => { return res.status(HTTP_RESPONDE_BAD_REQUEST).send(e); }); }); 我想知道如何testing上面提到的路线,确保用户login。 这是我目前的testing(它没有通过,因为我无法发送authentication: it("Testing protected route", done => { chai .request(server) .post("/api/test") .send(test) .end((err, res) => { expect(res.status).to.equal(200); done(); }); }); 我已经尝试了以下,但是当我运行testing他们redirect到login页面。 it("Testing protected route", done => { […]

柴没有达到.end()

我正在使用摩卡和柴来testing我的节点/快递API,我不明白为什么testing没有达到.end() 这是testing: it('should authenticate successfully with user credentials', function (done) { agent .post('/login') .set('Content-Type', 'application/x-www-form-urlencoded') .send({ 'username': 'username', 'password': 'password'}) .end(function (err, res) { console.log(res); console.log('***************************Authenticated*********************************************'); expect(res).to.have.status(200); }); done(); }); 这里是我打的路线: app.post('/login', passport.authenticate('ldapauth', { successRedirect: '/' })); 我想我的问题可能是事实上没有正式的回应,而是一个redirect,但我不知道如何处理它。

摩卡testing表示启动

如果缺less参数(例如数据库URI),我的快速服务器会抛出一个错误。 我想在摩卡testing它实际上抛出了错误,但我不知道如何让摩卡这样做。 if(!parameters.db) { throw new Error('Please provide a db URI'); } 我有一个像这样的testing设置: it('Throws Error when no db URI provided in production mode', function () { expect(require('../server')).to.throw(); done(); }); 当我的快速应用程序抛出错误,错误被扔进控制台和testing失败(实际上,它不会结束)。 我遇到的另一个问题是,如果环境处于生产模式,Express仅检查参数。 我试图在testing套件中设置stream程环境到生产环境,但是当我运行它时, NODE_ENV仍然被设置为“testing”。 before(function() { env = process.env; // This doesn't really set the environment when I run the tests. process.env.NODE_ENV = 'production'; });

我将如何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,我讨厌它..很多。 我正在努力学习基础知识,这是我所经历的最令人沮丧的学习曲线。 我一直在查找文件几个小时,仍然无法弄清楚如何检查哪条路线已被渲染

在使用Chai-HTTP进行开机时,似乎有错误的内容types

我期待利用Chai-HTTP进行一些testing。 当然,我想比我的GET更多的testing,但是当我尝试做POST时,我似乎遇到了一个重大的障碍。 在试图找出为什么我的POST不起作用,我开始打他们对POSTtesting服务器。 这是一个POST尝试,使用完全不同的工具链(Jasmine-Node和Frisby)进行testing(工作得很好): frisby.create('LOGIN') .post('http://posttestserver.com/post.php', { grant_type:'password', username:'helllo@world.com', password:'password' }) .addHeader("Token", "text/plain") .expectStatus(200) }) .toss(); 其结果是: Time: Mon, 27 Jun 16 13:40:54 -0700 Source ip: 204.191.154.66 Headers (Some may be inserted by server) REQUEST_URI = /post.php QUERY_STRING = REQUEST_METHOD = POST GATEWAY_INTERFACE = CGI/1.1 REMOTE_PORT = 19216 REMOTE_ADDR = 204.191.154.66 HTTP_CONNECTION = close CONTENT_LENGTH […]

用Mocha和ChaitestingExpress和护照OAuth2

我有一个使用Express和Passport的应用程序。 我正在使用Passport的Google OAuth2策略。 我有几条路线需要通过这个策略login。 目前我正在使用Mocha和Chai进行集成testing,但是我不确定如何绕过或使用某些路由所需的OAuth2身份validation。 例如,这是我的一个testing: it("should list a single item on /items/<id> GET", function(done) { chai.request(server) .get('/items/' + id) .end(function(err, res) { res.should.have.status(200); res.should.be.json; res.body.should.be.a('object'); res.body.should.have.property('description'); done(); }); }); 我的路线为/items/:id router.get('/items/:id', auth.isLoggedIn, function(req, res) { var item = getItem(); res.json(item); }); /items/:id需要login。 有没有办法绕过logintesting,或嘲笑一个用户,我的集成testing将工作?

编写unit testing使用child_process的节点服务

我有一个节点服务,我使用child_process spwan一个孩子,在我的情况下,孩子是一个C ++二进制文件,需要STDIN和streamSTDOUT。 该服务正在按预期工作,现在我正在尝试使用mocha / chai在服务中编写unit testing,并通过伊斯坦布尔运行testing。 我已经为这些stuufs奠定了基础,但只想获得如何为使用child_process的节点服务编写unit testing的示例。 基于这个例子,我可以尝试为我的服务编写unit testing,并在Instanbul上运行它们来生成覆盖率报告。 服务产生这样的孩子, var spawn = require('child_process').spawn, child = spawn(pathToBinary); child.stdin.write('JSON'); child.stdout.on('data', function (data) { //perform operations here });

用摩卡柴和Stormpath

我的谷歌福可能失败了我,但我正在考虑为使用Stormpath进行身份validation的快速应用程序构buildunit testing。 所以典型的路线看起来像这样; app.get('/api/user', stormpath.loginRequired, userApi.get); 在这种情况下,我将如何使用像Mocha + Chai这样的testing框架?

我如何构buildnode.js摩卡testing

我非常喜欢在node.js中进行unit testing,我想知道在node.js中编写unit testing的最佳做法是什么,比如'it'方法可以有多less个asserttesting用例,有没有什么标准只用一个方法写一个testing用例。 请给我一个写unit testing用例的想法。 提前致谢。:)