Tag: unit testing

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,我的方法可能会很费力。

如何使空的占位符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?

反应unit testing在es6中开玩笑

我在反应世界很新,并试图写简单的朋友列表应用程序。 我用es6风格写了我的朋友商店,并使用babel作为从es5到es6的编译器。 import AppDispatcher from '../dispatcher/app_dispatcher'; import { EventEmitter } from 'events'; import FRIENDS_CONST from '../constants/friends'; const CHANGE_EVENT = 'CHANGE'; let friendsList = []; let add = (name) => { let counter = friendsList.length + 1; let newFriend = { id: counter, name: name }; friendsList.push(newFriend); } let remove = (id) => { let index […]

用摩卡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 = […]

把整个class级用sintesting

序言:我读过很多关于SO和博客的文章,但还没有看到任何回答这个问题的东西。 也许我只是在寻找错误的东西… 假设我正在开发一个将在Widget对象上运行的WidgetManager类。 如何使用sinon来testingWidgetManager是否正确使用Widget API,而无需拉动整个Widget库? 基本原理:WidgetManager的testing应该从Widget类中分离出来。 也许我还没有写Widget,或者Widget是一个外部库。 无论哪种方式,我应该能够testingWidgetManager是正确使用Widget的API,而无需创build真正的Widgets。 我知道sinon mocks只能在现有的类上工作,而且据我所知,sinon stubs也需要这个类存在,才能被截断。 为了使其具体,我将如何testingWidget.create()正在调用一个参数名称在下面的代码一次? 代码正在testing中 // file: widget-manager.js function WidgetManager() { this.widgets = [] } WidgetManager.prototype.addWidget = function(name) { this.widgets.push(Widget.create(name)); } testing代码 // file: widget-manager-test.js var WidgetManager = require('../lib/widget-manager.js') var sinon = require('sinon'); describe('WidgetManager', function() { describe('#addWidget', function() { it('should call Widget.create with the correct name', function() […]

在所有使用摩卡的testing之后,在哪里删除数据库并closures连接

我试图找出到哪里放置一个函数来删除数据库,并在所有testing运行后closures连接。 这里是我的嵌套testing: //db.connection.db.dropDatabase(); //db.connection.close(); describe('User', function(){ beforeEach(function(done){ }); after(function(done){ }); describe('#save()', function(){ beforeEach(function(done){ }); it('should have username property', function(done){ user.save(function(err, user){ done(); }); }); // now try a negative test it('should not save if username is not present', function(done){ user.save(function(err, user){ done(); }); }); }); describe('#find()', function(){ beforeEach(function(done){ user.save(function(err, user){ done(); }); }); it('should find […]

我如何testing摩卡未被捕获的错误?

我想testing下面的函数按预期执行: function throwNextTick(error) { process.nextTick(function () { throw error; }); } 这是我的尝试: describe("throwNextTick", function () { it("works as expected", function (next) { var error = new Error("boo!"); var recordedError = null; process.once("uncaughtException", function (error) { recordedError = error; }); throwNextTick(error); process.nextTick(function () { recordedError.should.be(error); next(); }); }); }); 但摩卡似乎想保留任何错误本身,并得到它们时,我的testing失败: C:\Users\ddenicola\Programming (Synced)\pubit>mocha test/basicTest.js throwNextTick 0) works […]

Chai unittesting – expect(42).to.be.an('integer')

根据http://chaijs.com/api/bdd/#a ,可以使用a / an来检查variables的types。 。一种) @param{ String } type @param{ String } message _optional_ a和an断言是可以用作语言链或声明一个值types的别名。 但是,我无法检查variablesbeeing整数。 给定的例子,例如expect('1337').to.be.a('string'); 为我工作,但以下不: expect(42).to.be.an('integer'); expect(42).to.be.an('Integer'); expect(42).to.be.an('int'); expect(42).to.be.an('Int'); 运行摩卡时,他们都给我以下错误: Uncaught AssertionError: expected 42 to be an integer 我如何用chai来testing一个整数的variables?

嘲笑/ st Mong模式保存方法

给定一个简单的mongoose模型: import mongoose, { Schema } from 'mongoose'; const PostSchema = Schema({ title: { type: String }, postDate: { type: Date, default: Date.now } }, { timestamps: true }); const Post = mongoose.model('Post', PostSchema); export default Post; 我想testing这个模型,但是我遇到了一些障碍。 我目前的规格看起来像这样(为了简洁,省略了一些东西): import mongoose from 'mongoose'; import { expect } from 'chai'; import { Post } from '../../app/models'; […]