0

这是我的功能叫生活方式

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)

如何使功能正在传递?

4

1 回答 1

0

使用以下内容:

expect(res.body).to.deep.equal(expectedReply);
于 2017-02-14T18:06:26.130 回答