AppName::Application.configure do # Spork 使用時にクラスをキャッシュしないようにする if defined?(Spork) && Spork.using_spork? config.cache_classes = false else config.cache_classes = true end end
🏀 RSpecの設定
RailsへのRSpecのインストールとひな型を作成。
rails generate rspec:install
.RSpecにはRSpecコマンドの引数を記述できます。次のとおり編集。
(詳細はRSpec –helpを参照)
echo '--color --drb -f d' > .rspec
RSpecの設定ファイルspec/spec_helper.rbを次のように変更。
require 'rubygems' require 'spork' require "webmock/rspec" WebMock.allow_net_connect! #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. ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'shoulda-matchers'
# Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# database_cleanerでテストのたびにDBをクリアする require 'database_cleaner' config.before(:suite) do DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean_with(:truncation) end
config.before(:each) do DatabaseCleaner.start end
config.after(:each) do DatabaseCleaner.clean end end end
# This code will be run each time you run your specs. Spork.each_run do # Spork利用時にapp配下/locale/routesをリロード if Spork.using_spork? Rails.application.reloaders.each{|reloader| reloader.execute_if_updated } end end
# hirbの設定 begin require 'hirb' rescue LoadError # Missing goodies, bummer end
if defined? Hirb # Slightly dirty hack to fully support in-session Hirb.disable/enable toggling Hirb::View.instance_eval do def enable_output_method @output_method = true @old_print = Pry.config.print Pry.config.print = proc do |output, value| Hirb::View.view_or_page_output(value) || @old_print.call(output, value) end end
def disable_output_method Pry.config.print = @old_print @output_method = nil end end
Hirb.enable end
😀 Sporkの設定
sporkの初期設定。
bundle exec spork --bootstrap
sporkのテスト。(起動が確認できたらCtrl + Cで停止させてください)
bundle exec spork > Using RSpec > Preloading Rails environment > Loading Spork.prefork block... > Spork is ready and listening on 8989! # ← 成功
🎳 Guardの設定
Guardfileの作成。
bundle exec guard init
Guardfileに次の項目を追加。
guard 'rspec' do # rakeファイルの更新監視 watch(%r{^lib/(.+)\.rake$}) { |m| "spec/lib/#{m[1]}_rake_spec.rb" } end
Guard uses Growl to send notifications.
Guard uses TerminalTitle to send notifications.
Guard is now watching at '/path/to/app/test-app/'
LiveReload 1.6 is waiting for a browser to connect.
Guard::RSpec is running
No examples found.
Finished in 0.00007 seconds
0 examples, 0 failures