简体   繁体   English

Node Express应用程序-结构单元/集成测试

[英]Node express app - structuring unit / integration tests

I'm trying to set up my test suite for my nodejs / express application. 我正在尝试为我的nodejs / express应用程序设置测试套件。

I have a structure similar to this example, where I have an app folder, within which are model, view, controller folders. 我有一个类似于示例的结构,其中有一个应用程序文件夹,其中包含模型,视图,控制器文件夹。

I have my test directory currently split as 我的测试目录当前为

  • unit 单元
  • integration 积分

Inside unit, I mimic the folder structure of app. 在单元内部,我模仿应用程序的文件夹结构。 I have unit tests around my modes etc... that save to a test mongodb instance 我的模式等周围都有单元测试...保存到测试mongodb实例

My question is, should I unit tests my controllers, or should I save that for integration testing? 我的问题是,应该对控制器进行单元测试,还是应该将其保存以进行集成测试?

My 'controllers' look like: 我的“控制器”如下所示:

controllers/account.js 控制器/account.js

exports.login = function(req, res) {
     res.render('account/login', {
          title: 'Log In'
     });
};

exports.login_post = function(req, res, next) {
     passport.authenticate('local', function(err, user, info) {
          if (err) {
               return next(err);
          }
          if (!user) {
               return res.redirect('/account/login');
          }
          req.logIn(user, function(err) {
               if (err) {                   
                    return next(err);
               }
               return res.redirect('/');
          });
     })(req, res, next);
};

Would it be better to test this by using supertest, across the whole stack, or by “unit” testing login_post for example? 通过在整个堆栈中使用supertest还是通过例如对login_post进行“单元”测试来更好地进行测试?

I will keep this for the integration tests. 我将保留此用于集成测试。 I'll also go with tools like Phantom or Dalekjs . 我还将使用PhantomDalekjs之类的工具。 My experience shows me that it is better to test the actual rendered html, but following a specific user interaction flow. 我的经验告诉我,最好测试实际呈现的html,但要遵循特定的用户交互流程。 There are of course problems with this approach. 这种方法当然存在问题。 In my case was the fact that I had to change the markup very often. 就我而言,这是我不得不经常更改标记的事实。 This of course break the tests all the time. 当然,这总是会破坏测试。

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

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