Node.js 将摩卡茶与Stormpath一起使用

Node.js 将摩卡茶与Stormpath一起使用,node.js,express,mocha.js,chai,Node.js,Express,Mocha.js,Chai,我的google fu可能让我失望,但我正在考虑为使用Stormpath进行身份验证的Express应用程序构建单元测试。所以一条典型的路线看起来像这样 app.get('/api/user',stormpath.loginRequired,userApi.get) 在这种情况下,我将如何使用Mocha+Chai这样的测试框架?看看我在express stormpath库中编写的集成测试,例如:谢谢@rdeges,这让我找到了一个好的解决方案。我可以看到如何在stormpath中创建测试用户帐户

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

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


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

看看我在express stormpath库中编写的集成测试,例如:

谢谢@rdeges,这让我找到了一个好的解决方案。我可以看到如何在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();
                    });
            });
var-agent=chai.request.agent(服务器);
代理人
.post(“/login”)
.set('Accept','application/json')
.set('Content-Type','application/x-www-form-urlencoded')
.send({登录名:'',密码:'})
.然后(功能(res){
//“代理”现在保存了会话cookie,并将发送它
//在下一个请求中返回到服务器:
代理人
.put(“/user”)
.send({user:data})
.end((错误、恢复)=>{
如果(错误){
记录器错误(res.text);
}
res.应具有的状态(200);
//你在这里做测试吗
完成();
});
});