02Sep, 2020
Language blog :
English
Share blog : 
02 September, 2020
English

TDD with Ruby on Rails

By

4 mins read
TDD with Ruby on Rails

TDD ย่อมากจาก Test-Driven Development และหลักการของ TDD คือการเริ่มจากการเขียน Test ก่อนที่จะเขียน Code เพื่อให้ Test ผ่าน จากนั้น ก็ทำการ Refactor Code เพื่อให้อ่าน และเข้าใจง่ายยิ่งขึ้น ตามภาพข้างล่าง

  • จากการรัน Test รอบแรก Test จะพังทั้งหมด (RED)
  • จากนั้นเขียนโค้ดเพื่อให้ทดสอบผ่าน (GREEN)
  • และ Refactor Code ให้ผ่านง่ายขึ้น (REFACTOR)
  • แล้วก็ทำซ้ำๆๆ จนทดสอบผ่านทั้งหมด

ในบทความนี้เราจะมาทำ TDD ด้วย Rails On Rails และ Minitest ที่มากับ Rails มาลุยกันเลย

1.  เริ่มจากการสร้างแอปฯ​ ก่อน

$ rails new cats --skip-javascript

2. สร้าง Cat Model

$ rails generate model cat name

3. run migration

$ rails db:migrate

4. สร้าง Controller เพื่อจะทำรายการของแมว

$ rails g controller cats index

5. เขียน Test

# test/fixtures/cats.yml
one:
  name: British Shorthair

two:
  name: Scottish fold
# test/controllers/cats_controller_test.rb
require 'test_helper'

class CatsControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    get cats_url
    assert_response :success
  end

  test "should see cat list" do
    get cats_url
    assert_match "British Shorthair", @response.body
    assert_match "Scottish fold", @response.body
  end

  test "should create cat" do
    post cats_url, params: {
      cat: {name: "American Shorthair"}
    }
    assert_redirected_to cats_url
    assert_match "American Shorthair", @response.body
  end
end

6. มาลอง run test กัน

$ rails test
 rails test
Running via Spring preloader in process 93131
Run options: --seed 30862

# Running:

E

Error:
CatsControllerTest#test_should_get_index:
NameError: undefined local variable or method `cats_url' for #<CatsControllerTest:0x00007fbe91a54400>
    test/controllers/cats_controller_test.rb:5:in `block in <class:CatsControllerTest>'


rails test test/controllers/cats_controller_test.rb:4

E

Error:
CatsControllerTest#test_should_see_cat_list:
NameError: undefined local variable or method `cats_url' for #<CatsControllerTest:0x00007fbe91a54400>
    test/controllers/cats_controller_test.rb:10:in `block in <class:CatsControllerTest>'


rails test test/controllers/cats_controller_test.rb:9



Finished in 0.173663s, 11.5166 runs/s, 0.0000 assertions/s.
2 runs, 0 assertions, 0 failures, 2 errors, 0 skips

แล้ว Test ก็พังตามที่เราคิดไว้ จากนั้นเราก็มาเขียน Code เพื่อ ทำให้ Test ผ่าน

7. จาก  "undefined local variable or method `cats_url' คือไม่มี path เราต้องไปแก้ที่ `config/routes.rb`

Rails.application.routes.draw do
  resources :cats
end

8. run test

$ rails test
Running via Spring preloader in process 93217
Run options: --seed 65184

# Running:

.F

Failure:
CatsControllerTest#test_should_see_cat_list [/Users/theeraphatjantakat/sennalabs/cats/test/controllers/cats_controller_test.rb:11]:
Expected /British\ Shorthair/ to match "<!DOCTYPE html>\n<html>\n  <head>\n    <title>Cats</title>\n    \n    \n\n    <link rel=\"stylesheet\" media=\"all\" href=\"/assets/application-04c3ae28f07e1bf734223bf526d0cdd296440ef53bcb3f80b9f093c6bf02f747.css\" />\n  </head>\n\n  <body>\n    <h1>Cats#index</h1>\n<p>Find me in app/views/cats/index.html.erb</p>\n\n  </body>\n</html>\n".


rails test test/controllers/cats_controller_test.rb:9



Finished in 0.284871s, 7.0207 runs/s, 10.5311 assertions/s.
2 runs, 3 assertions, 1 failures, 0 errors, 0 skips

9. แล้วก็เขียนโค้ด

# app/controllers/cats_controller.rb
class CatsController < ApplicationController
  def index
    @cats = Cat.all
  end
end
# app/views/cats/index.html.erb
<% @cats.each do |cat| %>
  <%= cat.name %>
<% end %>

10.  แล้วก็ run test

rails test
Running via Spring preloader in process 93423
Run options: --seed 52351

# Running:

..

Finished in 0.193870s, 10.3162 runs/s, 25.7905 assertions/s.
2 runs, 5 assertions, 0 failures, 0 errors, 0 skips

และแล้ว Test ทั้งหมดก็ผ่าน และสังเกตได้ว่า ตั้งแต่ข้อ 5 - 10 คือการทำ TDD (RED -> GREEN -> REFACTOR) เนื่องจาก Code ชุดนี้ค่อนข้างเล็กและเข้าใจง่ายทำให้ไม่ต้อง Refactor Code

จบไปแล้วกับการทำ TDD ซึ่งการที่เราทำ TDD จะทำให้เราเขียน develop งานได้ตรงตาม requirement ของลูกค้าเนื่องจากเราเขียน Test อิงตาม use case ต่างๆ จาก requirement และการทำ TDD จะทำให้มี Code Coverage เพิ่มขึ้นอีกด้วย แต่ในทางกลับกันถ้าเราเข้าใจ requirement หรือ used case ผิดแล้วมาเขียน Test ก็ให้ code ที่เราเขียนผิดได้เช่นกัน

Written by
Senna Labs
Senna Labs

Subscribe to follow product news, latest in technology, solutions, and updates

- More than 120,000 people/day visit to read our blogs

Other articles for you

25
July, 2024
JS class syntax
25 July, 2024
JS class syntax
เชื่อว่าหลายๆคนที่เขียน javascript กันมา คงต้องเคยสงสัยกันบ้าง ว่า class ที่อยู่ใน js เนี่ย มันคืออะไร แล้วมันมีหน้าที่ต่างกับการประกาศ function อย่างไร? เรามารู้จักกับ class ให้มากขึ้นกันดีกว่า class เปรียบเสมือนกับ blueprint หรือแบบพิมพ์เขียว ที่สามารถนำไปสร้างเป็นสิ่งของ( object ) ตาม blueprint หรือแบบพิมพ์เขียว( class ) นั้นๆได้ โดยภายใน class

By

4 mins read
Thai
25
July, 2024
15 สิ่งที่ทุกธุรกิจต้องรู้เกี่ยวกับ 5G
25 July, 2024
15 สิ่งที่ทุกธุรกิจต้องรู้เกี่ยวกับ 5G
ผู้ให้บริการเครือข่ายในสหรัฐฯ ได้เปิดตัว 5G ในหลายรูปแบบ และเช่นเดียวกับผู้ให้บริการเครือข่ายในยุโรปหลายราย แต่… 5G มันคืออะไร และทำไมเราต้องให้ความสนใจ บทความนี้ได้รวบรวม 15 สิ่งที่ทุกธุรกิจต้องรู้เกี่ยวกับ 5G เพราะเราปฏิเสธไม่ได้เลยว่ามันกำลังจะถูกใช้งานอย่างกว้างขวางขึ้น 1. 5G หรือ Fifth-Generation คือยุคใหม่ของเทคโนโลยีเครือข่ายไร้สายที่จะมาแทนที่ระบบ 4G ที่เราใช้อยู่ในปัจจุบัน ซึ่งมันไม่ได้ถูกจำกัดแค่มือถือเท่านั้น แต่รวมถึงอุปกรณ์ทุกชนิดที่เชื่อมต่ออินเตอร์เน็ตได้ 2. 5G คือการพัฒนา 3 ส่วนที่สำคัญที่จะนำมาสู่การเชื่อมต่ออุปกรณ์ไร้สายต่างๆ ขยายช่องสัญญาณขนาดใหญ่ขึ้นเพื่อเพิ่มความเร็วในการเชื่อมต่อ การตอบสนองที่รวดเร็วขึ้นในระยะเวลาที่น้อยลง ความสามารถในการเชื่อมต่ออุปกรณ์มากกว่า 1 ในเวลาเดียวกัน 3. สัญญาณ 5G นั้นแตกต่างจากระบบ

By

4 mins read
Thai
25
July, 2024
จัดการ Array ด้วย Javascript (Clone Deep)
25 July, 2024
จัดการ Array ด้วย Javascript (Clone Deep)
ในปัจจุบันนี้ ปฏิเสธไม่ได้เลยว่าภาษาที่ถูกใช้ในการเขียนเว็บต่าง ๆ นั้น คงหนีไม่พ้นภาษา Javascript ซึ่งเป็นภาษาที่ถูกนำไปพัฒนาเป็น framework หรือ library ต่าง ๆ มากมาย ผู้พัฒนาหลายคนก็มีรูปแบบการเขียนภาษา Javascript ที่แตกต่างกัน เราเลยมีแนวทางการเขียนที่หลากหลาย มาแบ่งปันเพื่อน ๆ เกี่ยวกับการจัดการ Array ด้วยภาษา Javascript กัน เรามาดูตัวอย่างกันเลยดีกว่า โดยปกติแล้วการ copy ค่าจาก value type ธรรมดา สามารถเขียนได้ดังนี้

By

4 mins read
Thai

Let’s build digital products that are
simply awesome !

We will get back to you within 24 hours!Go to contact us
Please tell us your ideas.
- Senna Labsmake it happy
Contact ball
Contact us bg 2
Contact us bg 4
Contact us bg 1
Ball leftBall rightBall leftBall right
Sennalabs gray logo28/11 Soi Ruamrudee, Lumphini, Pathumwan, Bangkok 10330+66 62 389 4599hello@sennalabs.com© 2022 Senna Labs Co., Ltd.All rights reserved.