Run Tests Faster and Running Faster Tests

Running Faster Spec is Important...

Running Faster Spec is Important...

  • You need rapid feedback to get the full benefit of the TDD process.

 

  • Your will lose focus if the test takes more than 10 seconds to run.

 

  • So it is important that you get the test results as fast as possible.

Approaches

  • Run Smaller Group of Tests

 

  • Run Rails in the Background

Run Smaller Group of Tests 1

rspec 指令接受多個 argument,所以我可以指定只跑某一個 (或多個) RSpec 檔:

 

$ rspec spec/models/category_spec.rb spec/models/video_spec.rb

Run Smaller Group of Tests 2

若今天我要跑的 unit test 是散佈在一個 RSpec 檔案裡很多地方,可針對想跑的 unit tests 下 tag:

 

# 我對這個 unit test 下了一個叫 :focus 的 tag
it 'if none of the videos found, return an empty array', :focus => true do 
end

it 'should return video with title that matches the user input title' do
end

Run Smaller Group of Tests 3

接下來在 rspec 指令加上 "--tag" 即可 :

 

$ rspec --tag focus

或是你像跳過這組 spec :
 

$ rspec --tag ~focus

Run Rails in the Background 1

RSpec 每次執行都需要重開 rails,而 rails 啟動若花太多時間,原本一個只要一秒就跑完的 RSpec 檔會多花超過十秒,解法之一就是讓 Rails 跑在 background 裡面

 

Run Rails in the Background 2

Spring 是 rails core team 指名的官方 background preloader

 

# 在 gemfile 輸入
gem "spring", group: :development
gem spring-commands-rspec

Run Rails in the Background 3

用 Spring 跑 rspec:

 

$ bundle exec spring rspec

Run Rails in the Background 4

Zeus Gem - 另一個非常強大的 preloader

 

$ gem install zeus

Run Rails in the Background 4

啟動 Zeus :

 

$ zeus start

接下來就有多個 command 可以使用:

zeus rake
zeus runner (alias: r)
zeus console (alias: c)
zeus server (alias: s)
zeus generate (alias: g)
zeus destroy (alias: d)
zeus dbconsole

Run Rails in the Background 5

接下來就可用 Zeus 跑 RSpec:
 

$ zeus rspec spec/models/client_spec.rb

記得使用 zeus 需要修改 spec_helper.rb:

require 'rspec/rails'

RSpec.configure do |config|
...
...
...
end

deck

By Eugene Chang