How does one unit test routes with Express?

后端 未结 8 2158
一整个雨季
一整个雨季 2020-12-07 08:38

I\'m in the process of learning Node.js and have been playing around with Express. Really like the framework;however, I\'m having trouble figuring out how to write a unit/i

相关标签:
8条回答
  • 2020-12-07 09:37

    The easiest way to test HTTP with express is to steal TJ's http helper

    I personally use his helper

    it("should do something", function (done) {
        request(app())
        .get('/session/new')
        .expect('GET', done)
    })
    

    If you want to specifically test your routes object, then pass in correct mocks

    describe("Default Route", function(){
        it("should provide the a title and the index view name", function(done){
            routes.index({}, {
                render: function (viewName) {
                    viewName.should.equal("index")
                    done()
                }
            })
        })
    })
    
    0 讨论(0)
  • 2020-12-07 09:38

    I've come to the conclusion that the only way to really unit test express applications is to maintain a lot of separation between the request handlers and your core logic.

    Thus, your application logic should be in separate modules that can be required and unit tested, and have minimal dependence on the Express Request and Response classes as such.

    Then in the request handlers you need to call appropriate methods of your core logic classes.

    I'll put an example up once I've finished restructuring my current app!

    I guess something like this? (Feel free to fork the gist or comment, I'm still exploring this).

    Edit

    Here's a tiny example, inline. See the gist for a more detailed example.

    /// usercontroller.js
    var UserController = {
       _database: null,
       setDatabase: function(db) { this._database = db; },
    
       findUserByEmail: function(email, callback) {
           this._database.collection('usercollection').findOne({ email: email }, callback);
       }
    };
    
    module.exports = UserController;
    
    /// routes.js
    
    /* GET user by email */
    router.get('/:email', function(req, res) {
        var UserController = require('./usercontroller');
        UserController.setDB(databaseHandleFromSomewhere);
        UserController.findUserByEmail(req.params.email, function(err, result) {
            if (err) throw err;
            res.json(result);
        });
    });
    
    0 讨论(0)
提交回复
热议问题