【问题标题】:Using Mocha Chai with Stormpath在 Stormpath 中使用 Mocha Chai
【发布时间】:2016-10-16 14:57:54
【问题描述】:

我的 google-fu 可能让我失望了,但我正在研究为使用 Stormpath 进行身份验证的 Express 应用程序构建单元测试。所以典型的路线看起来像这样;

app.get('/api/user', stormpath.loginRequired, userApi.get);

在这种情况下,我将如何使用像 Mocha + Chai 这样的测试框架?

【问题讨论】:

    标签: node.js express mocha.js chai


    【解决方案1】:

    感谢@rdegges,这让我找到了一个好的解决方案。我可以看到如何在 Stormpath 中创建一个测试用户帐户,但就我而言,我很高兴已经在 Stormpath 中设置了一个测试用户。所以我只需要创建会话,然后进行实际的测试运行(所以我只需要 Chai 来保持会话)。所以这是我最终得到的解决方案。

            var agent = chai.request.agent(server);
    
            agent
               .post('/login')
               .set('Accept', 'application/json')
               .set('Content-Type', 'application/x-www-form-urlencoded')
               .send({ login: '<username>', password: '<password>' })
               .then(function (res) {
    
                   // The `agent` now has the session cookie saved, and will send it
                   // back to the server in the next request:
                   agent
                        .put('/user')
                        .send({user:data})
                        .end((err, res) => {
    
                            if (err){
                                Logger.error(res.text);
                            }
    
                            res.should.have.status(200);
    
                            // Do testing here
                            done();
                        });
                });
    

    【讨论】:

      【解决方案2】:

      看看我在 express-stormpath 库中为此编写的集成测试,例如:https://github.com/stormpath/express-stormpath/blob/master/test/middlewares/test-groups-required.js#L208

      【讨论】:

        猜你喜欢
        • 2017-06-05
        • 2021-02-14
        • 1970-01-01
        • 1970-01-01
        • 2013-04-11
        • 2019-02-04
        • 2020-10-11
        • 2019-02-15
        • 2015-11-03
        相关资源
        最近更新 更多