iT邦幫忙

2021 iThome 鐵人賽

DAY 23
0
自我挑戰組

Rails測試寫起乃!!!系列 第 23

Day23 測試寫起乃 - Spork

  • 分享至 

  • xImage
  •  

spork 為加速測試用套件,透過啟用 DRB server 載入環境讓你在執行測試時只需要載入一次環境,速度會快上不少

安裝 Spork

由於公司專案在 rails4 會遇上一個問題 Missing or uninitialized constant: ActiveModel::Observing (NameError) 在此找到解決方法,不過如果是 rails5 以上應該可以直接 gem 'spork'

gem 'spork', :github => 'sporkrb/spork' and bundle install

先下 spork --bootstrap 載入預設配置

接下來到 spec_helper.rb 修改

#spec_helper.rb

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.

# --- Instructions ---
# Sort the contents of this file into a Spork.prefork and a Spork.each_run
# block.
#
# The Spork.prefork block is run only once when the spork server is started.
# You typically want to place most of your (slow) initializer code in here, in
# particular, require'ing any 3rd-party gems that you don't normally modify
# during development.
#
# The Spork.each_run block is run each time you run your specs.  In case you
# need to load files that tend to change during development, require them here.
# With Rails, your application modules are loaded automatically, so sometimes
# this block can remain empty.
#
# Note: You can modify files loaded *from* the Spork.each_run block without
# restarting the spork server.  However, this file itself will not be reloaded,
# so if you change any of the code inside the each_run block, you still need to
# restart the server.  In general, if you have non-trivial code in this file,
# it's advisable to move it into a separate file so you can easily edit it
# without restarting spork.  (For example, with RSpec, you could move
# non-trivial code into a file spec/support/my_helper.rb, making sure that the
# spec/support/* files are require'd from inside the each_run block.)
#
# Any code that is left outside the two blocks will be run during preforking
# *and* during each_run -- that's probably not what you want.
#
# These instructions should self-destruct in 10 seconds.  If they don't, feel
# free to delete them.

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'


require File.expand_path('../config/environment', __dir__)
require 'rspec/rails'
require 'rspec/collection_matchers'
require 'webmock/rspec'
# 所有 require 基本上都可以進來

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
 # 自己原本 config的設定
end

Spork.each_run do
  # 在每次執行spork的時候要執行那些東西

end

基本上可以直接用Spork.prefork全部包起來

我們再到 .rspec 新增參數 --drb

#.rspec
--require spec_helper
-- color
-- drb

之後到 terminal 下 spork 如果看到以下就代表已啟動 server

Spork is ready and listening on pid code!

這時我們執行測試後 log 會進入到 spork 視窗 而不是原本 rspec 這邊
且你也會看到他只載入一次環境,是直接開始跑測試而不是又要 reload 一次,如果是一些大專案的話載入環境至少都要十幾秒以上...真的快不少!

# rspec spec/controllers/customers_controller_spec.rb
Running tests with args ["--color", "--require", "byebug", "--require", "pry", "--require", "spec_helper", "spec/controllers/admin/customers_controller_spec.rb"]...

#log here...

Finished in 8.37 seconds (files took 43.06 seconds to load)
9 examples, 0 failures

Done.

上一篇
Day22 測試寫起乃 - Sidekiq Testing
下一篇
Day24 測試寫起乃 - Guard
系列文
Rails測試寫起乃!!!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言