Day23 测试写起乃 - Spork

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.

<<:  第23天 - 延续昨天代替的东西_文件审核系统(1)_建表

>>:  [Day15] swift & kotlin 实作篇!(6) Layout排版

Azure - Day4 Cosmos DB

Hi~大家好,我今天想要分享的实作包括如下: < Cosmos DB > 1. 如何建立...

大共享时代系列_028_云端串流游戏 ( Cloud Gaming )

云端串流游戏加速推坑的开始~ (∩^o^)⊃━☆゚.*・。 线上游戏跟云端串流游戏的差异? 线上游戏...

DAY17 机器学习专案实作-员工离职预测(中)

ㄧ、资料前处理 1. 补值、删值 前面我们透过视觉化的方式找到资料有缺值,因此我们要将资料进行补值。...

Day 18 | FPS灭火AR游戏开发Part3 - 火焰生成

昨天的文章中已介绍火焰粒子的制作,那麽今天的文章将会说明如何在AR世界中产生火焰! 火焰产生器 在场...

Unity与Photon的新手相遇旅途 | Day25-Unity build测试Photon房间

今天内容为把我们做好的Unity打包出去执行,并且同时打开後测试房间以及连线。 ...