Tag: 摩卡

Sinon不要模仿db-mysql Node.js库

我试图在Node.js中使用Sinon和Mocha来模拟我的数据库进行testing。 我已经尝试了以下内容: var sinon = require('sinon'); var mysql = require('db-mysql'); beforeEach(function() { var db = sinon.mock(mysql); db.expects('execute'); }); 但我不断收到以下错误: TypeError: Attempted to wrap undefined property execute as function 我假设这是嘲笑类而不是数据库实例。 所以我通过做var db = sinon.mock(new mysql.Database());嘲笑实例var db = sinon.mock(new mysql.Database()); 代替。 当我这样做的时候,那么db-mysql实例的所有有效方法都会传递,比如db.connect()和db.query() ,不pipe参数是什么。 我无法设置行为。 为了设置行为,我试图在db上调用.expects但是我得到以下错误: TypeError: Object [object Object] has no method 'expects' 什么是设定预期行为的正确方法? 此外,如何testing同一function的几种行为? 我是否需要根据testing的期望在每个testing中做到这一点?

摩卡在testing中保存文件状态

我有一个unit testing,正在testingconfiguration文件的更新…当然,我运行testing后,我的文件现在改变了。 我想我可以使用“之前”来caching文件,并在“之后”恢复。 mod = require('../modtotest'); describe('Device Configuration', function(){ var confPath = '../config/config.json'; var config; before(function(){ //cache object config = require(confPath); }) describe('Update Config', function(){ it('Should update config', function(done){ mod.updateConfig(); //do assertions }) }); after(function(){ //restore fs.writeFileSync(confPath, JSON.stringify(config, null, 4)); }) }) 但是,每当我尝试这样做,它说该文件不存在。 看起来,当我运行Mocha( -> app $mocha -R spec )时,它会在我执行它的地方执行全局安装目录吗? 有一个简单的方法来实现我想要的? 或者我可能只是把它全部弄错了?

如何testingREST API +客户端MVC应用程序?

如果你有一个RESTful服务器,它只通过从数据库获取一些信息来响应JSON,然后你有一个客户端应用程序,比如Backbone,Ember或者Angular,你从哪一边testing一个应用程序? 我是否需要两个testing – 一个用于后端testing,另一个用于前端testing? 我问的原因是testingREST API本身是一种困难。 考虑这个代码示例(使用Mocha,Supertest,Express): var request = require('supertest'); var should = require('chai').should(); var app = require('../app'); describe('GET /api/v1/people/:id', function() { it('should respond with a single person instance', function(done) { request(app) .get('/api/v1/people/:id') .expect(200) .end(function(err, res) { var json = res.body; json.should.have.property('name'); done(); }); }); }); 注意:id在URL中的:id ? 这是一个特定人物的ObjectId。 我怎么知道要通过那里? 在这一点上,我甚至没有看过数据库。 我的意思是我需要导入Person模型,连接到数据库,并从testing内部做查询? 也许我应该把我的整个app.js移动到testing中? (讽刺:P)。 […]

我可以使用伊斯坦布尔为摩卡html-cov的仪器

我正在尝试使用istanbul instrument输出mocha -R html-cov但不能得到它的工作。 我的testing脚本是: test: post: – node_modules/.bin/istanbul cover node_modules/.bin/_mocha -dir $CIRCLE_ARTIFACTS — -u exports -R spec – node_modules/.bin/istanbul instrument . -o lib-cov – cp package.json lib-cov/ # needs to be copied manually – MY_LIB_COV=1 node_modules/.bin/mocha -R html-cov > $CIRCLE_ARTIFACTS/coverage.html 我的testing有以下开关: var mylib = process.env.MY_LIB_COV ? require('../lib-cov') : require('../'); 伊斯坦布尔在lcov-report/index.html给了我一个很好的输出。 摩卡的html-cov在coverage.html给了我'0%覆盖率0 SLOC' 伊斯坦布尔的lcov报告是丑陋的。 我想用漂亮的mocha […]

如何通过摩卡testing运行时参数?

我正在使用摩卡来testing我在node.js写的一些代码。 我的一个程序被devise为从运行时参数的命令行运行。 由于几个选项是目录,代码将validation选项并确定目录是否存在,包括可选的基本目录。 这是我想用摩卡testing的东西,但是代码并不是被devise成可以被导入的,因为它被devise成从命令行运行。 所以我的问题是,如何通过摩卡testing程序的命令行参数? 我尝试了谷歌search,但唉,我不断收到关于摩卡的文章,提到摩卡的命令行,而不是如何testingnode.js代码的命令行。

摩卡和锡诺方法间谍

我正在为我的应用程序设置testing,并且我希望检查使用Sinon的x次方法,我的testing框架是Mocha。 我怎么能做到这一点,下面是我想testing的代码,我期待确保recursiveRequest被createClients调用x次。 Nodezilla.prototype.createClients = function(batched, i){ var self = this; if(batched) batchRequests(); if(batched == false && i < this.virtualUsers){ // set timeout to help prevent out of memory errors setTimeout( function() { self.createClients(false, i+1); }, 0 ); }else{ this.recursiveRequest(); } }; Nodezilla.prototype.recursiveRequest = function(){ var self = this; self.hrtime = process.hrtime(); if(!this.halt){ self.reqMade++; this.http.get(this.options, function(resp){ […]

向Chai / Mocha提供应包含的部分密钥列表

看来,如果我这样做 describe( 'Add Youtube', function () { it( 'should return the video data, including user, title and content fields', function ( done ) { this.timeout( 5000 ) request({ method: 'POST', url: 'https://localhost:8443/api/add', json: true, strictSSL: false, body: { "type": "youtube", "url": "https://www.youtube.com/watch?v=uxfRLNiSikM" }, headers: { "Authorization": "Bearer " + newTestUser.token } }, function ( err, […]

Nodejstesting图像调整与摩卡

我已经使用graphicmagick来调整我的nodejs应用程序中的图像的大小。 问题是编写unit testing的时候,我似乎找不到任何方向或例子。 是否有道理,我testing图像resize,看到我使用第三方模块? 如果是的话,我怎么能写我的代码testing? // dependencies var async = require('async'); var AWS = require('aws-sdk'); var gm = require('gm').subClass({ imageMagick: true }); var util = require('util'); // get reference to S3 client var s3 = new AWS.S3(); var _800px = { width: 800, destinationPath: "large" }; var _500px = { width: 500, destinationPath: "medium" }; […]

我如何将自定义数据传递给我的摩卡testing?

我们正在使用酱汁实验室进行跨平台/跨浏览器testing。 你可以在这个要点find我的testing套件(以节省这个问题的空间): https : //gist.github.com/chevex/397a5a18a1a386897b41 问题是,我能弄清楚如何将自定义数据传递给testing套件的唯一方法是通过一个环境variables 。 因此,当我的gulp任务试图并行运行testing套件对多个目标时,它们最终都会针对同一个目标运行,因为循环完成迭代,并且process.env.SAUCE_TARGET被设置为第一个套房甚至运行。 var gulp = require('gulp'); var gulpMocha = require('gulp-mocha'); var mergeStream = require('merge-stream'); gulp.task('sauce-tests', function () { var targets = ['chrome', 'firefox', 'internet explorer']; var streams = targets.map(function (target) { process.env.SAUCE_TARGET = target; return gulp.src('./test/sauce-tests.js', {read:false}) .pipe(gulpMocha({ reporter: 'spec' }); }); return mergeStream.apply(null, streams); }); forEach提供的闭包没有帮助,因为它在每次迭代中设置一个基本上全局的值( process.env.SAUCE_TARGET )。 […]

Webstorm:debugging在ES6中编写的摩卡testing

在使用WebStorm 10进行Node.js开发时,我无法在debugging时逐步完成testing用例(用ES6编写)。 我猜测编译文件会使testing文件不同步。 想知道是否有人能够成功debugging? 这是我的test.js文件 describe('TestFlow', function () { "use strict"; it('Test Path', (done) => { console.log("Testing_1 happy path\n"); console.log("Testing_2 happy path\n"); done(); }); }); 我已经configuration了Mocha选项来使用–compilers js:babel / register 。 现在,当我尝试debugging代码时,逐步过程是不可预知的,并且不起作用。 babel编译混淆了debugging过程。 请问我是否有解决此问题的方法? 我认为debuggingES6代码是WebStrom 10中的一个function,但我没有运气