Supertest · ตรวจ Response Contract
Response Body
อ่านและตรวจข้อมูลหลักที่ API ส่งกลับใน response body
Response body คือข้อมูลที่ caller ใช้ต่อ
หลังจาก status บอกผลลัพธ์ระดับ protocol แล้ว response body บอกข้อมูลของ resource หรือ error ที่ caller จะนำไปใช้ต่อ ใน Supertest เราอ่านผ่าน `response.body` เมื่อ server ส่ง JSON ที่ parser เข้าใจ
เขียน assertion จาก contract ไม่ใช่ implementation
ถ้า id ถูกสร้างโดย database อย่า assert เป็นเลขตายตัวเว้นแต่ fixture กำหนดไว้ สิ่งที่สำคัญกว่าคือ id เป็น number, title สะท้อน input และ completed มี default ที่ถูกต้อง
กฎสำคัญ
public
assert field ที่ caller ต้องใช้
public id, title และ completed เป็นหลักฐานที่เหมาะกับ create response
stable
อย่าตรวจ internal field โดยบังเอิญ
field เช่น version หรือ createdAt ควร assert เฉพาะเมื่อ API contract ประกาศให้ caller พึ่งพาได้