1

I use the following code in my client to check the response:

 assert.strictEqual(data,{'status':'ok','message':'tweet received'});

This is the code I wrote when sending the message:

 res.send({status:'ok',message:'tweet recieved'});

This is the error I got:

 assert.js:92
 throw new assert.AssertionError({
    ^
 AssertionError: "{\n  \"status\": \"ok\",\n  \"message\": \"tweet recieved\"\n}" === {"status":"ok","message":"tweet received"}
at IncomingMessage.<anonymous> (/home/anr/Desktop/node js/mvc/ntwittertest.js:22:10)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183

1 Answers1

2

The response what you have got, is in String format. You might have to do JSON.parse to get the JavaScript object.

JSON.parse(data)

But, to compare two objects you might want to do JSON.stringify on both of them and compare them as strings.

JSON.stringify(JSON.parse(data)) === JSON.stringify(other_object)

Even this method has its own issues. Strict comparing two objects will be successful only if both the expressions point to the same object. For example,

var obj1 = {};
var obj2 = {};
var obj3 = obj1;
console.log(obj1 === obj2);
console.log(obj1 === obj3);

Even though obj1 and obj2 are identical, they are not the same. So, it will return false. But in the second case, they both refer to the same object. That is why it returns true.

Read about Object comparison, in this question to get more information.

I would recommend, parsing the JSON string to a valid JavaScript object and then do member level comparison.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497