[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-go-unit-testing-all--*":3,"academy-blog-translations-ave1lh1t81d9pmv":89},{"data":4,"page":77,"perPage":77,"totalItems":77,"totalPages":77},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":83,"keywords":84,"locale":59,"published_at":85,"scheduled_at":13,"school_blog":81,"short_description":86,"slug":87,"status":79,"title":6,"updated":88,"updated_by":13,"views":82},"EP.10 Go กับ Unit Testing - เขียนโค้ดให้มั่นใจ ด้วยการทดสอบ!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go กับ Unit Testing - เขียนโค้ดให้มั่นใจ ด้วยการทดสอบ!\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>การทดสอบเป็นส่วนสำคัญของการพัฒนาโปรแกรมที่มีคุณภาพ\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Unit Testing คืออะไร?\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Unit Test คือการทดสอบส่วนย่อยๆ ของโปรแกรม เพื่อให้มั่นใจว่าทำงานถูกต้องตามที่คาดหวัง โดยใช้แพ็กเกจ testing ใน Go\u003Cbr>ตัวอย่าง Unit Test\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport \"testing\"\r\n\r\nfunc add(a, b int) int {\r\n    return a + b\r\n}\r\n\r\nfunc TestAdd(t *testing.T) {\r\n    result := add(2, 3)\r\n    if result != 5 {\r\n        t.Errorf(\"Expected 5, got %d\", result)\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การรัน Unit Tests\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>ใช้คำสั่งนี้เพื่อรันการทดสอบใน Go:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">go test\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การทดสอบหลายกรณีด้วย Table-Driven Tests\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Go มีแนวทางการเขียนทดสอบแบบ Table-Driven เพื่อทดสอบหลายๆ กรณีในครั้งเดียว\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func TestAddCases(t *testing.T) {\r\n    cases := []struct {\r\n        a, b, expected int\r\n    }{\r\n        {1, 2, 3},\r\n        {2, 3, 5},\r\n        {0, 0, 0},\r\n    }\r\n\r\n    for _, c := range cases {\r\n        result := add(c.a, c.b)\r\n        if result != c.expected {\r\n            t.Errorf(\"Expected %d, got %d\", c.expected, result)\r\n        }\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Test-Driven Development (TDD) คืออะไร?\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>TDD คือกระบวนการเขียนเทสท์ก่อน แล้วจึงเขียนโค้ดให้ผ่านเทสท์นั้น ช่วยให้มั่นใจว่าโค้ดที่พัฒนามีคุณภาพและทำงานได้ถูกต้องตั้งแต่แรก\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>วงจรของ TDD:\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cul>\u003Cli>เขียน Unit Test ที่ล้มเหลว (Red)\u003C\u002Fli>\u003Cli>เขียนโค้ดให้ผ่านการทดสอบ (Green)\u003C\u002Fli>\u003Cli>ปรับปรุงโค้ด (Refactor)\u003C\u002Fli>\u003C\u002Ful>\u003Cp>การ Mock สำหรับทดสอบฟังก์ชันที่เรียกใช้ภายนอก\u003Cbr>การ Mock เป็นเทคนิคที่ช่วยจำลองพฤติกรรมของการเรียกใช้ฟังก์ชันภายนอก ทำให้ทดสอบได้อย่างแม่นยำ\u003C\u002Fp>","19_11zon_pgnis76hto.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Frc8qlkixp2g2adh\u002F19_11zon_pgnis76hto.webp","2026-03-04 08:33:59.553Z","",{"keywords":15,"locale":53,"school_blog":63},[16,23,28,33,38,43,48],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:26:59.195Z","gab60xd583s3qaw","Superdev School","2026-04-10 16:07:37.087Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:33:57.434Z","hz551yc9f7fozes","การพัฒนาแอปพลิเคชัน","2026-04-10 16:08:04.227Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:33:57.712Z","z6rhic75w1tdqh9","Table-Driven Tests","2026-04-10 16:08:04.322Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:33:57.054Z","g54iztrb8jaqawk","Testing","2026-04-10 16:08:04.159Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:33:55.996Z","xcm9atm8go2opqr","TDD","2026-04-10 16:08:03.582Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:33:54.199Z","tcmhcek6qmvgjns","Unit Testing","2026-04-10 16:08:03.006Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"code":54,"collectionId":55,"collectionName":56,"created":57,"flag":58,"id":59,"is_default":60,"label":61,"updated":62},"th","pbc_1989393366","locales","2026-01-22 10:59:55.832Z","twemoji:flag-thailand","s8wri3bt4vgg2ji",true,"Thai","2026-04-10 15:42:46.614Z",{"category":64,"collectionId":65,"collectionName":66,"expand":67,"id":81,"views":82},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":68},{"blogIds":69,"collectionId":70,"collectionName":71,"created":72,"created_by":13,"id":64,"image":73,"image_alt":13,"image_path":74,"label":75,"name":76,"priority":77,"publish_at":78,"scheduled_at":13,"status":79,"updated":80,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":76,"th":76},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","ave1lh1t81d9pmv",461,"rc8qlkixp2g2adh",[20,25,30,35,40,45,50],"2025-01-24 17:21:04.883Z","เรียนรู้การเขียน Unit Tests ด้วย testing, การรันและตรวจสอบผลลัพธ์, การใช้ Table-Driven Tests และ TDD ในภาษา Go","go-unit-testing","2026-04-25 02:47:29.185Z",{"th":87}]