nock,mocha,testing Uncaught AssertionError:

时间:2017-02-14 14:26:51

标签: javascript unit-testing mocking mocha nock

这是我的生活方式

var url="http://blah/get/[type]/[id]";
function lifeStyle(req,res){
    var dataEnter={
        'id':1797,
        'type':'lifestyle'
    }
    var p = promiseStyle(dataEnter);
    p.then(function(data) {
        res.send(data);
    }).catch(function(err) {
        console.log(err);
    })
}
function promiseStyle(dataEnter){
    var urlData = url.replace("[type]",dataEnter.type).replace("[id]",dataEnter.id);
    var prom=new Promise (function(resolve,reject){
        http.get(urlData,function(res) {
            var data = "";
            res.setEncoding('utf8');             
            res.on("data", function(chunk) {    
                data += chunk;                    
            })                                  
            res.on("end", function() {         
                resolve(JSON.parse(data));                  
            });                                
            res.on("error", function(error) {
                console.log("Got error: " + error.message);
                reject(error);
            });
        });
    })
    return prom;
}

exports.lifeStyle=lifeStyle;
exports.lifeStyleId=lifeStyleId;

这是我的功能测试文件我在其中使用了mocha和nock测试,但它没有通过,尽管res.body等于预期的对象

  describe("function lifeStyle",function(){
  it("should response is equal expected",function(done){
        var scope=nock('http://blah/get')
                  .get('/lifestyle/1797')
                  .reply(200,expectedReply)
            request(server).get('/lifestyle')
            .end(function(err, res) {
              if(err){
                console.log(err);
                done(err);
              }else{
                console.log("res.body: ",  res.body,res.status);
                console.log("expectedReply ",  expectedReply);
                expect(res.status).to.equal(200);
                expect(res.body).to.equal(expectedReply);
                scope.done();
                done(); 
              }
           });

        });
      });

我收到了这个错误

function lifeStyle
    res.body:  { location: { id: 1797, forecast: { step: [Object] }, name: 'city' } } 200
    expectedReply  { location: { id: 1797, forecast: { step: [Object] }, name: 'city' } }
        1) should response is equal expected


      0 passing (63ms)
      1 failing

      1) function lifeStyle should response is equal expected:

          Uncaught AssertionError: expected { Object (location) } to equal { Object (location) }
          + expected - actual


          at Test.<anonymous> (specs/lifeStyleSpec.js:84:37)
          at Test.assert (node_modules/supertest/lib/test.js:179:6)
          at Server.assert (node_modules/supertest/lib/test.js:131:12)
          at emitCloseNT (net.js:1524:8)

如何让功能传递?

1 个答案:

答案 0 :(得分:0)

使用以下内容:

expect(res.body).to.deep.equal(expectedReply);
相关问题