How does one unit test routes with Express?

后端 未结 8 2190
一整个雨季
一整个雨季 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()
                }
            })
        })
    })
    

提交回复
热议问题