首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mocha TDD,Chai-http不记录任何响应

Mocha TDD,Chai-http不记录任何响应
EN

Stack Overflow用户
提问于 2017-03-15 02:42:55
回答 2查看 556关注 0票数 2

我正在尝试使用chai-http测试身份验证api,但是我无法获得此特定块的任何响应-

代码语言:javascript
复制
describe('Authenticate user', () => {
it('Should Authenticate user', () => {
    let server = require(process.cwd() + "/server.js")
    let token
    chai.request(server)
        .post('/api/authenticate')
        .send({
            "userid": "Jeetendra",
            "password": "abcd1234"
        })
        .then(res => {
            console.log('Inside authenticate result block')
            console.log(res)
            token = res.body.data
        },err => {
           console.log('Inside authenticate error block')
            // token = res.body.data
            console.log(err)
        })
  })
})

对于上面的代码块,我没有打印任何日志。有人能帮帮忙吗?我错过了什么?

EN

回答 2

Stack Overflow用户

发布于 2017-03-15 21:23:57

你的代码似乎是异步运行的。您需要将done回调传递给测试用例,然后调用它以标记测试完成

代码语言:javascript
复制
it('Should Authenticate user', (done) => {
    let server = require(process.cwd() + "/server.js")
    let token
    chai.request(server)
        .post('/api/authenticate')
        .send({
            "userid": "Jeetendra",
            "password": "abcd1234"
        })
        .then(res => {
            console.log('Inside authenticate result block')
            console.log(res)
            token = res.body.data
            done()
        },err => {
           console.log('Inside authenticate error block')
            // token = res.body.data
            console.log(err)
            done(err)
        })
  })
票数 2
EN

Stack Overflow用户

发布于 2017-08-20 19:03:11

摩卡咖啡

摘自Mocha docs

不鼓励

向Mocha传递箭头函数(“lambda”)。由于它的词法绑定,这些函数无法访问Mocha上下文。

使用done()几乎扼杀了Promise的全部用途。通常,只需在it中将=>更改为function(){}即可解决此问题。

代码语言:javascript
复制
describe('Authenticate user', () => {
  it('Should Authenticate user', function(){ // use function rather than arrow
   let server = require(process.cwd() + "/server.js")
   let token
   chai.request(server)
     .post('/api/authenticate')
     .send({
       "userid": "Jeetendra",
       "password": "abcd1234"
     })
    .then(res => {
      console.log('Inside authenticate result block')
      console.log(res)
      token = res.body.data
    },err => {
      console.log('Inside authenticate error block')
      // token = res.body.data
      console.log(err)
    })
  })
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42794077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档