Afgu/unit testing 2

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
หน้านีเป็นส่วนหนึ่งของชุดแบบฝึกหัด Agile from the ground up

เนื้อหา

ใช้ของเก่าหากินหน่อยครับ เอกสารมาจาก01219343-55

แบบฝึกหัด

หา examples

  • แบ่งกลุ่ม ใช้ script ที่แจกให้ห้อง (ต้นฉบับจะมาแปะที่นี่ต่อไป)

Isolation

สมมติว่าเราต้องการจะเขียนฟังก์ชัน

function popularSpell(words) {
}

ที่นำอาร์เรย์ของสตริง words ไปค้นใน Google แล้วนับจำนวน แล้วคืนคำที่มีจำนวนผลลัพธ์การค้นมากที่สุด

โค้ดของฟังก์ชันดังกล่าวอาจจะมีขั้นตอนประมาณดังด้านล่าง

function popularSpell(words) {
  // 1. for each word w, do
  // 2.   generate google query url for w
  // 3.   make a request to google, get the result content
  // 4.   extract the number of results from the content
  // 5.   update the word with the maximum number
}

คำถามคือเราจะ test function ดังกล่าวได้อย่างไร?

ถามกลับกันคือ จากฟังก์ชันดังกล่าว มีอะไรเป็น อุปสรรค ในการ test ฟังก์ชันดังกล่าวบ้าง?

แยก dependencies

stubs

mocks/test spies

เราจะใช้ Sinon.JS ซึ่งเป็น test spies ในการทดลองเขียน