2017-02-27 54 views
0

我用摩卡,Supertest柴以下测试:摩卡测试通过用不正确的数据

const app = require('../server') 
const expect = require('chai').should() 
const request = require('supertest') 

describe('GET /webhook', function() { 
    context('a verify request from Facebook with a correct token', function() { 
    it('responds with 200 and displays the challenge query in the response body', function() { 
     request(app) 
     .get('/webhook?hub.mode=subscribe&hub.challenge=123456789&hub.verify_token=' + process.env.FB_VERIFY_TOKEN) 
     .expect(200) 
     .end(function (err, res) { 
      console.log(res.text) 
      res.should.have.property("text", "abc") 
      done() 
     }) 
    }) 
    }) 
}) 

正如你所看到的,我们期待的回应与内容属性“文本” 'ABC'。但是,当我运行测试时,它通过没有任何问题。

下面是响应的缩短版:

ClientRequest{ 
    res:IncomingMessage { 
     headers:{ 
     'x-powered-by':'Express', 
     'content-type':'text/html; charset=utf-8', 
     'content-length':'9', 
     etag:'W/"9-JfnnlDI7RTiF9RgfG2JNCw"', 
     date:'Mon, 27 Feb 2017 19:45:30 GMT', 
     connection:'close' 
     }, 
     rawHeaders:[ 
     'X-Powered-By', 
     'Express', 
     'Content-Type', 
     'text/html; charset=utf-8', 
     'Content-Length', 
     '9', 
     'ETag', 
     'W/"9-JfnnlDI7RTiF9RgfG2JNCw"', 
     'Date', 
     'Mon, 27 Feb 2017 19:45:30 GMT', 
     'Connection', 
     'close' 
     ], 
     upgrade:false, 
     url:'', 
     method:null, 
     statusCode:200, 
     statusMessage:'OK', 
     req:[ 
     Circular 
     ], 
     text:'123456789', 
    } 
} 

我期待测试失败,因为ABC = 123456789,可惜事实并非如此。

有没有人有任何想法?

+0

不知道这一点,但它看起来对我来说,'res.should.have .property('text','abc')'需要改为'expect(res).to.have.property('text','abc');'注意你也必须改变'const expect = require('chai')。should()'to'const expect = require('chai');' –

回答

2

只要你有异步操作,你需要告诉测试运行,以等待要完成异步操作。使用mocha你可以从测试中返回一个承诺,或者你可以使用done回调或者任意调用它。

在你的情况,而你在呼唤done回调,你是不是通过它来测试回调,应该是:

it('should bla bla', function (done) { 
    ... 
    // call done() 
}) 
+0

哦,我的话。我显然已经过了太久。感谢您的发现! – tombraider