Tag: 摩卡

需要外部js文件进行摩卡testing

所以我和我的express.js项目一起玩BDD和摩卡。 我刚刚开始,所以这是我作为我的第一个testing用例: should = require "should" require "../lib/models/skill.js" describe 'Skill', -> describe '#constructor()', -> it 'should return an instance of class skill', -> testSkill = new Skill "iOS", "4 years", 100 testSkill.constructor.name.should.equal 'Skill' (也这个coffeescript生成一些奇怪的js,因为它插入返回到最后的声明..这是正确的方式来设置一个testing与咖啡脚本?) 现在当我运行摩卡时,我得到这个错误: 1) Skill #constructor() should return an instance of class skill: ReferenceError: Skill is not defined 我认为这意味着skill.jsinput不正确。 我的技能类非常简单,只是一个构造函数: class Skill constructor: […]

下午摩根看不起

我有一个非常简单的Koa应用程序: var app = module.exports = require("koa")(); app.use(function *(){ this.body = "Koa says Hi!"; }); var port = process.env.PORT || (process.argv[2] || 3000); port = (typeof port === "number") ? port : 3000; app.listen(port); console.log("Application started. Listening on port:" + port); 我用这样的摩卡和超级特质来testing var app = require("../"); var request = require("supertest").agent(app.listen()); describe("Our amazing site", function () […]

Mocha.js:即使在testing套装失败的情况下也可以运行“之后”

即使其中一个testing(套件)失败,是否可以运行“之后”钩子?

循环摩卡testing?

我试图循环一个摩卡testing套件(我想testing我的系统与无数的值与预期的结果),但我不能得到它的工作。 例如: spec / example_spec.coffee : test_values = ["one", "two", "three"] for value in test_values describe "TestSuite", -> it "does some test", -> console.log value true.should.be.ok 问题是我的控制台日志输出如下所示: three three three 我希望它看起来像这样: one two three 我如何循环这些值为我的摩卡testing?

分离unit testing和集成testing

我有兴趣创build完整的模拟unit testing,以及集成testing,检查是否一些asynchronous操作已正确返回。 我想要一个unittesting的命令,一个用于integrationtesting的命令,这样我可以在我的CI工具中单独运行它们。 什么是最好的方法来做到这一点? 像摩卡这样的工具和玩笑似乎只关注一种做事方式。 我看到的唯一select是使用摩卡,并在目录中有两个文件夹。 就像是: __unit__ __integration__ 然后,我需要一些方法告诉mocha运行src目录中的所有__unit__testing,另一个告诉它运行所有的__integration__testing。 思考?

在下一次testing之前让摩卡等待

有一些摩卡testing,需要以前的函数调用数据,但是,因为它使用的是web服务,并希望它在运行下一个testing之前等待一段预定的时间,如下所示: var global; it('should give some info', function(done) { run.someMethod(param, function(err, result) { global = result.global done(); }); }); wait(30000); // basically block it from running the next assertion it('should give more info', function(done) { run.anotherMethod(global, function(err, result) { expect(result).to.be.an('object'); done(); }); }); 任何想法,将不胜感激。 谢谢!

摩卡如何知道只有我的asynchronoustesting等待和超时?

当我使用Mocha进行testing时,通常需要运行asynchronous和同步testing的组合。 摩卡处理这个美丽的让我指定一个callback, done ,只要我的testing是asynchronous的。 我的问题是,摩卡如何在内部观察我的testing,并知道它应该等待asynchronous活动? 它似乎随时等待我的testing函数中定义的callback参数。 您可以在下面的示例中看到,第一个testing应该超时,第二个应该继续并在user.save调用匿名函数之前完成。 // In an async test that doesn't call done, mocha will timeout. describe('User', function(){ describe('#save()', function(){ it('should save without error', function(done){ var user = new User('Luna'); user.save(function(err){ if (err) throw err; }); }) }) }) // The same test without done will proceed without timing out. describe('User', function(){ […]

使用Mochatesting框架的HTML报告

我一直在使用NodeJS和Mocha生成一些testing,我想find一种方法将结果放到浏览器中。 我知道摩卡支持使用'html'记者和mocha init <dir>但是这两者似乎都不适合我(记者实际上甚至没有运行testing就抛出错误)。 有人能给我一个通过Mocha运行testing并生成HTML报告的好例子吗?我想模仿的一个例子是在visionmedia网站上。 另外,为了例子,我们会说我正在使用名为example.js的testing文件。 在此先感谢您的帮助,但令人惊讶的是这里有很less的例子。

在摩卡testing预期的失败

使用摩卡,我试图testing一个构造函数是否抛出一个错误。 我还没有能够使用期望的语法做到这一点,所以我想要做到以下几点: it('should throw exception when instantiated', function() { try { new ErrorThrowingObject(); // Force the test to fail since error wasn't thrown } catch (error) { // Constructor threw Error, so test succeeded. } } 这可能吗?

用Mocha和SupertesttestingExpress错误

例如,我必须在验收testing中testing服务器错误(Express),例如,验收testing不能(或不应该)发送响应 错误:发送后无法设置标题。 捕获error handling程序的错误和5XX代码响应将提供有价值的反馈在这里,但问题是头已经发送。 这类错误可能并不严重,难以发现,通常可以从日志中找出。 规范是 it('should send 200', function (done) { request(app).get('/').expect(200, done); }); 和testing的应用程序是 app.get('/', function (req, res, next) { res.sendStatus(200); next(); }); app.use(function (req, res) { res.sendStatus(200); }); 快速app实例和请求testing库(即Supertest)在类似情况下进行通信的最适合的方式是什么? 问题不限于Supertest。 如果有包可以解决Supertest无法解决的问题,也可以考虑。