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

查看:26
本文介绍了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)
  • 以尽可能 DRY 的方式执行此操作,即不为每个描述块创建拆解前/后块

?

推荐答案

我会尽量回答你的.

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

为此,我假设您使用 Mocha 作为测试框架,您可以像这样简单地使用 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();
        });
    });
});

基本上我在这里所做的是在每个 it 之前清理我的数据库,但你可以让它做任何你想做的事情.

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 的方式执行此操作,即不创建before/after 块,每个描述块的拆卸

我不确定我是否得到了您想要的,但请查看文档中的钩子 beforeafterbeforeEachafterEach.我想你会在这里找到你想要的.

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天全站免登陆