Shoulda เป็นอีกหนึ่งเฟรมเวิร์คสำหรับทดสอบโค้ดซึ่งมีจุดประสงค์หลักสองอย่างคือ:
ผู้เขียนเฟรมเวิร์คนี้คือ thoughtbot, ซึ่งเขียน Shoulda (testing framework), Factory Girl (ซึ่งสร้างข้อมูลสำหรับทดสอบที่ใช้โดย test suites), และ Paperclip (ผู้เขียน file uploads)
$ sudo gem install thoughtbot-shoulda -s http://gems.github.com
require 'rubygems' gem 'thoughtbot-shoulda' require 'shoulda'
Shoulda มีเอกสาร RDoc ที่คุณสามารถ อ่านได้ออนไลน์
ถ้าคุณใช้ Test::Unit คุณสามารถใช้ should block เพื่อแทน test_ method ต่างๆได้:
class CalculatorTest < Test::Unit::TestCase should "add two numbers for the sum" do calculator = Calculator.new assert_equal 4, calculator.sum(2, 2) end end
การใช้สไตล์นี้ทำให้อ่านข้อผิดพลาดได้ง่ายขึ้นด้วย:
Failure: test: Calculator should add two numbers for the sum. (CalculatorTest)
นอกจากนั้น Shoulda ยังมีวิธีจัดการเทสเคสต่างๆเพื่อลดการซ้ำซ้อนด้วยโดยใช้ context blocks.
class CalculatorTest < Test::Unit::TestCase context "a calculator" do setup do @calculator = Calculator.new end should "add two numbers for the sum" do assert_equal 4, @calculator.sum(2, 2) end should "multiply two numbers for the product" do assert_equal 10, @calculator.product(2, 5) end end end
context blocks ทำให้คุณสามารถสร้าง setup method ที่ใช้ร่วมกันระหว่างหลายๆ should blocks อย่างในตัวอย่างด้านบน ออบเจ็คต์ Calculator ใหม่จะถูกสร้างขึ้นและแชร์กันระหว่างเทสเคสทั้งสอง:
test: a calculator should add two numbers for the sum. test: a calculator should multiply two numbers for the product.
Shoulda มีตัวช่วยเพื่อทดสอบ Rails แอพพลิเคชั่นได้ด้วย
ลองดูตัวอย่างสำหรับการทดสอบ validation และความสัมพันธ์ของโมเดล User:
class UserTest < Test::Unit::TestCase should_validate_presence_of :name, :phone_number should_not_allow_values_for :phone_number, "abcd", "1234" should_allow_values_for :phone_number, "(123) 456-7890" should_not_allow_mass_assignment_of :password should_have_one :profile should_have_many :dogs should_have_many :messes, :through => :dogs should_belong_to :lover end
สำหรับแต่ละตัวช่วย คุณสามารถส่ง hash เป็นพารามิเตอร์สุดท้ายได้
สำหรับรายชื่อตัวช่วยทั้งหมด ลองดู เอกสารอ้างอิง ActionRecord::Macros
จริงๆแล้ว macros ของ Shoulda ใช้ matchers
ตั้งแต่ Shoulda 2.0.7 ตัว ActiveRecord matchers ทำงานได้เหมือน RSpec และไม่ต้องใช้ dependency อื่นๆนอกจาก Ruby เอง:
describe User do it { should validate_presence_of(:name) } it { should validate_presence_of(:phone_number) } %w(abcd 1234).each do |value| it { should_not allow_value(value).for(:phone_number) } end it { should allow_value("(123) 456-7890").for(:phone_number) } it { should_not allow_mass_assignment_of(:password) } it { should have_one(:profile) } it { should have_many(:dogs) } it { should have_many(:messes).through(:dogs) } it { should belong_to(:lover) } end
ทีมผู้พัฒนา Shoulda กำลังทำงานเพื่อแปลง ActionController macros ของ Shoulda ให้ทำงานได้เทียบเท่ากับ RSpec อยู่เช่นกัน
นอกจากนี้ Shoulda ยังมี macros ต่างๆสำหรับใช้ทดสอบ controller อีกด้วย ตัวอย่างต่อไปนี้เป็นตัวอย่างการทดสอบ show action แบบ RESTful:
context "on GET to :show for first record" do setup do @user = Factory(:email_confirmed_user) get :show, :id => @user.id end should_assign_to :user, :equals => "@user" should_respond_with :success should_render_template :show should_not_set_the_flash end
หมายเหตุ: “ Factory” มาจาก Factory Girl และ ”:email_confirmed_user” มาจาก Clearance.
สำหรับรายชื่อ macros ทั้งหมด ดู เอกสารอ้างอิง ActionController::Macros
คุณสามารถขอฟีเจอร์และรายงานบั๊กได้ที่ บัญชี Lighthouse ของ Shoulda ซึ่งดูแลโดยผู้พัฒนาและผู้ใช้ Shoulda อย่างขยันขันแข็ง
ถ้าคุณมีคำถามใดๆเกี่ยวกับ Shoulda คุณสามารถถามไปที่ กลุ่มข่าวของ Shoulda ก็ได้
Discussion