Node.js / Express应用程序中的测试环境 [英] Test environment in Node.js / Express application

查看:209
本文介绍了Node.js / Express应用程序中的测试环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Node,并且我一直在跟随各种教程。

I've just starting working with Node, and I've been following along with various tutorials.

我创建了一个Express应用程序,并设置了Mongoose和Jasmine。

I've created an Express app, and setup Mongoose and Jasmine.

如何配置我的规格以便我可以:

How can I configure my specs so that I can:


  • 创建模型,在每个规范后自动清理它们

  • 使用用于创建测试对象的不同数据库(例如myapp_test)

  • 以尽可能干的方式执行此操作,即不创建具有每个描述块的拆卸的前/后块

  • create models, automatically clean them up after each spec
  • use a different database for creating test objects (say myapp_test)
  • do this in a way that is as DRY as possible, i.e. not creating a before / after block with the teardown for each describe block

推荐答案

尝试回答你。


创建模型,在每个规格后自动清理。

为了做到这一点,我会假设你使用摩卡作为测试框架,你可以简单地使用函数 beforeEach 像这样:

To do that I'll assume you use Mocha as the testing framework you can simply use the function beforeEach like this :

describe('POST /api/users', function() {
    beforeEach(function(done) {
        User.remove({}, function (err) {
            if (err) throw err;
            done();
        });
    });
});

基本上我在这里做的是在每个之前清除我的数据库,但可以让您做任何事情。

Basicly what I'm doing here is cleanning up my database before each it but you can make it do anything you want.


使用不同的数据库创建测试对象

这里,您应该使用节点 process.env 设置你的环境的方法这里是一篇文章,了解一下它是如何工作的。对于GRUNT项目来说很多,它可以帮助您完成工作流程和配置工作。

Here, you should use the node process.env method to setting your env. Here is a article to understand a little how it works. Take a lot to GRUNT projects, it helps a lot with your workflow and the configurations stuff.


是尽可能的DRY,即在每个描述块之前没有创建一个
块/拆分块/

我不知道我有没有你想要的,但是在之前的之后 beforeEach afterEach 。我想你会在这里找到你想要的东西。

I'm not sure I got what you want but take a look at the doc for the hooks before, after, beforeEach, afterEach. I think you will find what you want here.

这篇关于Node.js / Express应用程序中的测试环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆