简体   繁体   English

使用ExpressJS进行单元测试

[英]Unit Testing with ExpressJS

I am trying to unit test my ExpressJS routes. 我正在尝试对ExpressJS路由进行单元测试。 It looks something like this 看起来像这样

server.js server.js

var boards = require('./routes/BoardsRoute.js');
app.get('/api/v1/boards/:id', boards.getBoard);

BoardRoutes.js BoardRoutes.js

exports.getBoard = function(req, res) {
  BoardModel.find({
        name: 'Kanban Board'
    }, function(err, columns) {
    if (!err) {
        return res.send(columns);
    } else {
        return console.log(err);
    }
});
return res.send(board);
};

I would like to Mock out the BoardModel as this is the call to the Mongoose Model (aka Database call). 我想模拟出BoardModel,因为这是对猫鼬模型的调用(又名数据库调用)。 As I assume that unit tests should not be making calls to a database and have no server running. 我假设单元测试不应该调用数据库并且没有服务器在运行。

Should I be testing the getBoards completely seperatly to the server.js app.get() call. 我是否应该分别对server.js app.get()调用单独测试getBoards。 (As these requests apt.get will be covered by integration tests/e2e tests and they are HTTP requests) (由于apt.get这些请求将被集成测试/ e2e测试覆盖,它们是HTTP请求)

All the documentation and frameworks that I can see either have to have Express Server running in order to unit test the route and this particular exports.getBoard. 我可以看到的所有文档和框架都必须运行Express Server才能对路由和此特定的export.getBoard进行单元测试。

Things that I have tried to do, 我尝试做的事情

Use Sinon.js to mock a fakeServer so that I could test the HTTP Request and the method getBoard. 使用Sinon.js模拟一个假服务器,以便我可以测试HTTP请求和方法getBoard。

Use SuperAgent and Super Test to make the requests out to a server. 使用SuperAgent和Super Test将请求发送到服务器。 (I am uncomfortable with this as unit tests should not have a server running). (我对此感到不舒服,因为单元测试不应运行服务器)。

I am trying to use Mocha to test these routes. 我正在尝试使用Mocha来测试这些路线。

I would test that the route is wired up correctly first, then test that the call to the route does what you expect. 我首先要测试路由是否正确连接,然后再测试对路由的调用是否符合您的期望。

To test the route here's one way you could do it. 要测试路线,您可以采用以下一种方法。 Create a routes module that just wires up the routes: 创建一个路由模块,将路由连接起来:

var routes = {};                                                                                                           

routes.create = function(app) {                                                                                                                                                                                     
   app.get('/api/v1/boards/:id', boards.getBoard);
}

module.exports = routes;

You can then mock the app object and check the get method gets called correctly. 然后,您可以模拟app对象并检查get方法是否正确调用。 (I tend to use Sinon.js for this.) The create method will need to be called from your app module: (我倾向于为此使用Sinon.js。)create方法将需要从您的应用程序模块中调用:

var app = express();
... // set up express
routes.create(app);

Now you can focus on testing getBoard on its own like any other module. 现在,您可以像其他任何模块一样专注于自己测试getBoard了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM