ก่อนจะทำการ test ด้วย Rspec ก็็ต้องมีเครื่องมือช่วยกันหน่อย
ผมขอเลือกสภาพแวดล้อมที่เห็นว่ามันดีที่สุดละกันในการ test คือ ทำบน OS X แล้วใช้ tool ที่มหัศจรรย์อย่าง autotest ของ ZenTest และ Growl มาช่วย และอีกหนึ่งอย่างที่ผมชอบคือ RedGreen สำหรับ highlight ผลการ test บน terminal
ติดตั้งก่อน
$ sudo gem install rspec zentest redgreenแล้ว download growl มาติดตั้ง
$ open Growl-0.7.x.dmg $ cd /Volumes/Growl/Extras/growlnotify $ sudo ./install.shแล้วเพิ่ม PATH ตามที่มันบอกมาด้วย ยังไม่จบ ไป download icon สวยๆ มาก่อน จากที่นี่ก็ได้ หรือใครอยากได้เสียง ก็ลองหาโหลดดูครับ สมมติว่าโหลดมาเสร็จแล้ว extract จาก zip file เรียบร้อยแล้ว
$ mv your_image_path/autotest_images ~/.autotest_images $ nano .autotestใส่ไปว่า
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | module Autotest::Growl Autotest.add_hook :ran_command do |at| if at.results.empty? # break out gracefully if a test file throws an uncaught exception growl( "Syntax error", "", '~/.autotest_images/fail.png') else output = format_input( at.results ) display_notification( output ) end end private def self.growl title, msg, img, pri=0, stick="" system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{stick}" end # isolate the example, failure, pending line like "12 examples, 4 failures, 5 pending" # and remove color codes def self.format_input( input ) output = input.detect{ |result| result.include? "example"} output = remove_color_codes( output ) end def self.display_notification( output ) examples, failures, pending = output.split(", ") if failures.to_i > 0 # uncomment a line below for sound # system "~/bin/sndplay ~/your/sound/path/failed.wav -volume 0.5" growl( "Tests Failed", output, '~/.autotest_images/fail.png', 2 ) elsif pending.to_i > 0 growl( "Tests Pending", output, '~/.autotest_images/pending.png', 1 ) else growl( "Tests Passed", output, '~/.autotest_images/pass.png', 0 ) end end def self.remove_color_codes( string ) string.match( /m(.+)\e/)[1] end end |
ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails
หรือเพิ่ม -x ไป ถ้าใช้ subversion
แล้วมาลอง test เล่นๆ สักอัน
$ cd /your/app $ ruby script/generate rspec $ ruby script/generate rspec_scaffold article $ rake $ autotest
สั่งสร้างค่าพื้นฐานของ rspec ก่อน
แล้วสร้าง spec ของ resource ที่เรากำลังจะพิจารณา ซึ่งตอนนี้ ผมใช้ edge rails อยู่ ได้มีการ update อะไรหลายๆ อย่าง หนึ่งในนั้นคือ generator ซึ่ง scaffold จะทำการสร้างค่าพื้นฐานของ restful ไว้เลย
สั่ง rake เพื่อสร้างค่าพื้นฐานของสภาพแวดล้อมสำหรับ test
แล้วก็สั่ง autotest เพื่อเรียกให้ autotest ทำงาน
เปิด file ใน มาแก้สักตัว ก็น่าจะเห็น growl มันทำงาน และ การเปลี่ยนแปลงใน terminal
ก็มี screen shot เล็กๆ น้อย ให้ดูสำหรับ growl กับใน terminal
อันนี้ผมรันครั้งแรก
แล้วก็แก้ไขอะไรเล็กน้อยให้มันผ่าน
แล้วก็ลองเพิ่ม spec แต่ยังไม่ได้ implement
ข้อมูลจาก
internaut
Growl Notifier from Mike Vicent


