Tag: 摩卡

为什么在使用Bootstrap时,zombie.js浏览器会返回空的HTML?

我正在尝试使用Mocha和Zombie来testing我的Node.js / Express.js应用程序。 我可以通过简单的testing,但只要将Twitter Bootstrap的脚本标记添加到我的视图中,浏览器对象就会返回空的HTML。 这是我的testing代码: process.env.NODE_ENV = 'test'; var app = require('../app'); var assert = require('assert'); var Browser = require('zombie'); describe('home page', function() { before(function() { this.server = app.listen(3000); this.browser = new Browser({site: 'http://localhost:3000', debug : true}); }); it('should show welcome', function(done) { var browser = this.browser; browser .visit("/") .then(function() { console.log(browser.html()); }) .fail(function(error) […]

如何在基于Express的API上设置代码覆盖率?

我一直在这个问题一段时间,我不能让现有的解决scheme为我工作。 我有一个用Express.js编写的Node.js API。 我一直在使用Mocha,Chai和Supertest为API编写testing。 这些testing主要是集成testing。 一个testing可能看起来像: it('should fail to register a new user without the proper information', function(done) { api.post('/user') .send({}) .expect(400) .expect('Content-Type', /json/) .end(function(err, res) { should.exist(res.body); should.exist(res.body.error); should.not.exist(err); res.body.error.should.contain('Username'); res.body.error.should.contain('password'); done(); }); }); 实际的testing工作很好,但现在我需要能够查看这些testing的代码覆盖率。 我必须知道我没有做足够的testing。 我尝试过使用摩卡的testing范围: mocha -R html-cov –coverage > coverage.html 和伊斯坦布尔的: istanbul cover _mocha — -R spec –timeout 5000 两者都遭受同样的问题: 你看,这是一个示例路线(用户注册)。 […]

JavaScripttesting(摩卡)与'import'js文件

我理解module.export并require module.export : 需要外部js文件进行摩卡testing 虽然只要它是一个模块就可以使用,但我觉得这种方式不方便,因为我现在打算做的是在一个文件中testing代码。 例如,我有一个文件中的代码: app.js 'use strict'; console.log('app.js is running'); var INFINITY = 'INFINITY'; 现在,我想在一个文件中testing这个代码: test.js var expect = require('chai').expect; require('./app.js'); describe('INFINITY', function() { it('INFINITY === "INFINITY"', function() { expect(INFINITY) .to.equal('INFINITY'); }); }); testing代码执行app.js ,所以输出是; app.js is running 然后 ReferenceError: INFINITY is not defined 这不是我所期望的。 我不想使用module.export和写 var app = require('./app.js'); 和 testing代码中的每行都app.INFINITY和app.anyOtherValue 。 […]

如何用mocha / chai模拟窗口/文档

当我尝试unit testinggetElement函数 class BarFoo { getElement() { return document.querySelector('#barfoo'); } } 摩卡对document一无所知,所以我想你可能会这样做: beforeEach(() => { global.document = { querySelector: () => { … } } } 虽然这有效,但我想知道这是否是正确的方法,也许有解决这个问题的软件包,因为如果使用更多的浏览器API,我的方法可能会很费力。

在每个套件之前运行Mocha设置,而不是在每个testing之前

使用NodeJS和Mocha进行testing。 我想我明白before()和beforeEach()是如何工作的。 问题是,我想添加一个在每个“描述”之前运行的安装脚本,而不是在每个“它”之前运行。 如果我before()使用它,整个套件只运行一次,如果使用beforeEach()它会在每次testing之前执行,所以我试图find一个中间的基础。 所以,如果这是我的testing文件: require('./setupStuff'); describe('Suite one', function(){ it('S1 Test one', function(done){ … }); it('S1 Test two', function(done){ … }); }); describe('Suite two', function(){ it('S2 Test one', function(done){ … }); }); 我希望“setupStuff”包含一个在“套件一”和“套件二”之前运行的函数, 或者换句话说,在“S1testing一”和“S2testing一”之前,而不是在“S1testing二”之前。 可以这样做吗?

摩卡监控应用输出

我正在为nodejs web应用程序构build一个日志logging模块。 我想能够使用mochatesting,我的模块输出正确的消息到terminal 。 我一直在环顾四周,但还没有find任何明显的解决scheme来检查这一点。 我已经发现 process.stdout.on('data', function (){}) 但一直没有能够得到这个工作。 有没有人有任何build议?

如何在node.js中创build一个简单的套接字?

我试图创build一个虚拟套接字用于我的一些testing var net = require("net"); var s = new net.Socket(); s.on("data", function(data) { console.log("data received:", data); }); s.write("hello!"); 得到这个错误 错误:此套接字已closures。 我也试着创build套接字 var s = new net.Socket({allowHalfOpen: true}); 我究竟做错了什么? 作为参考,完整的testing看起来像这样 it("should say hello on connect", function(done) { var socket = new net.Socket(); var client = Client.createClient({socket: socket}); socket.on("data", function(data){ assert.equal("hello", data); done(); }); client.connect(); // writes […]

如何使空的占位符testing在摩卡故意失败?

我在NodeJS中编写一个API,并使用Mocha,Chai和SuperTest进行testing。 我正在使用一种典型的testing驱动方法来编写testing,然后使用工作代码来满足这些testing。 然而,由于所有不同排列的testing数量,我已经开始写空的占位符testing,以便我有所有的it('should…')描述来提醒我当我到达时要testing什么该function。 例如: it 'should not retrieve documents without an authorized user', (done) -> done() 这个问题是done()被调用,没有任何断言,所以testing被认为是传递,所以我已经添加了下面的断言。 false.should.equal true # force failure 但这是一个破解和摩卡显示失败的原因可能看起来很混乱,尤其是当其他完整的testing可能失败。 是否有任何官方的方式故意失败在摩卡这样的占位符testing?

Node.js + mocha + webdriverjs:失败的testingkill套件

我正在使用Mocha和WebDriverJStesting一个Web应用程序,或多或less地在这里描述 。 当testing通过,一切都很好。 但是,如果一个testing失败,套件中的其余testing将超时,跑步者将在套件结束时退出,而不closuresWebdriver实例。 示例testing用例: var assert = require('assert'), client = require("webdriverjs").remote({ logLevel: 'silent' }); describe('Self-test', function() { before(function(done) { client .init() .url('http://www.wikipedia.org/', function() { done(); }); }); after(function(done) { client.end(function() { done(); }); }); // tests it('should fail properly', function(done) { client.getTitle(function(result) { assert(false, 'This should fail'); done(); }); }); it('should pass afterwards', function(done) […]

用摩卡testing承诺链

我有以下风格的function(使用Node.JS下的蓝鸟承诺): module.exports = { somefunc: Promise.method(function somefunc(v) { if (v.data === undefined) throw new Error("Expecting data"); v.process_data = "xxx"; return module.exports.someother1(v) .then(module.exports.someother2) .then(module.exports.someother3) .then(module.exports.someother4) .then(module.exports.someother5) .then(module.exports.someother6); }), }); 我试图testing(使用摩卡,sinon,assert): // our test subject something = require('../lib/something'); describe('lib: something', function() { describe('somefunc', function() { it("should return a Error when called without data", function(done) { goterror = […]