【问题标题】:How to unit test google oauth passport in sails js with Mocha如何使用 Mocha 在 Sails js 中对 google oauth 护照进行单元测试
【发布时间】:2014-09-02 07:37:21
【问题描述】:

现在我正在尝试测试我的控制器,我需要访问会话,我发现您可以使用 superagent 登录,但我登录网络应用程序的唯一选择是通过 google oauth,现在我找不到任何合适的样本用于使用 Mocha 进行测试。有什么帮助吗?

【问题讨论】:

    标签: oauth tdd google-oauth sails.js mocha.js


    【解决方案1】:

    这取决于您如何实施会话。

    在我的 Sails 应用程序中,经过身份验证后,我设置了 req.session.authenticated = true,以及 cookie 等。如果你正在做类似的事情,你可以做的一件事是在你的 /login 路由中,添加:

    if (process.env.NODE_ENV === 'test') {
      req.session.authenticated = true;
      // do what you would do next after authentication
    } else {
      // do normal login procedures
    }
    

    然后,在您的测试中,在before 挂钩中,您可以使用superagent/login 路由发出请求以进行身份​​验证:

    describe('MyController', function() {
      var agent;
    
      before(function (done) {
        agent = require('superagent').agent('YOUR_APP_URL');
    
        // authenticate
        agent
          .post('/login')
          .end(done)
      });
    
      // in your tests, use the same agent to make future requests
      describe('#someAction', function() {
        it('should do something', function(done) {
          agent.
            .post('someAction')
            .end(function (err, res) {
              // should work!
            });
        });
      });
    });
    

    这只是一个想法 - 您可以调整此方法以适应您正在检查的会话。这适用于我使用 Mocha 进行测试的 Sails 应用程序。

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2016-10-23
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多