Test environment in Node.js / Express application

て烟熏妆下的殇ゞ 提交于 2019-12-09 00:36:11

问题


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

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

How can I configure my specs so that I can:

  • 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

?


回答1:


I'll try to answer you.

Create models, automatically clean them up after each spec.

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.

Use a different database for creating test objects

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.

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

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.



来源:https://stackoverflow.com/questions/24168592/test-environment-in-node-js-express-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!