TDD環境の構築手順をいつも忘れる為、備忘録的にメモしておこう。
RSpec2
まずは、皆さんご存知のRSpecを導入する。
Gemfileに以下を記述する。
Gemfile
gem 'rspec-rails', :group => [:development, :test]
RSpec2をインストールする。
$ bundle install
$ rails g rspec:install
create .rspec
create spec
create spec/spec_helper.rb
RSpecを実行する。
$ rspec
No examples found.
Finished in 0.00004 seconds
0 examples, 0 failures
無事にRspecが動きましたね。
Spork
次に、皆さんご存知のSporkを導入する。
Gemfileに以下を記述する。
Gemfile
group :test do
gem 'spork', '~> 0.9.0.rc'
end
Sporkをインストールする。
$ bundle install
$ spork --bootstrap
Using RSpec
Bootstrapping /Users/fakestarbaby/Projects/rails/devfun/spec/spec_helper.rb.
Done. Edit /Users/fakestarbaby/Projects/rails/devfun/spec/spec_helper.rb now with your favorite text editor and follow the instructions.
spec/spec_helper.rbを編集する。
spec/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.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# 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}
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
Sporkを実行する。
$ spork
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
無事にSporkが動きましたね。
.rspecを編集する。
.rspec
--colour
--drb
RSpecを実行する。
$ rspec
No examples found.
Finished in 0.00005 seconds
0 examples, 0 failures
無事にSporkを利用してRSpecが動きましたね。
Guard
最後に、皆さんご存知のGuardを導入する。
Gemfileに以下を記述する。
Gemfile
group :development do
gem 'growl'
gem 'guard'
gem 'guard-rspec'
gem 'guard-spork'
gem 'rb-fsevent', :require => false
end
Guardをインストールする。
$ bundle install
$ guard init spork
Writing new Guardfile to /Users/fakestarbaby/Projects/rails/devfun/Guardfile
spork guard added to Guardfile, feel free to edit it
$ guard init rspec
rspec guard added to Guardfile, feel free to edit it
Guardfileを編集する。
Guardfile
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/routes.rb')
watch(%r{^config/environments/.+\.rb$})
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
end
guard 'rspec', :version => 2, :cli => '--format nested --drb' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end
Guardを実行する。
$ guard
Guard uses Growl to send notifications.
Guard is now watching at '/Users/fakestarbaby/Projects/rails/devfun'
Starting Spork for RSpec
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
Spork server for RSpec successfully started
Guard::RSpec is running, with RSpec 2!
Running all specs
Running tests with args ["--color", "--failure-exit-code", "2", "--format", "nested", "--format", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--require", "/Users/fakestarbaby/.rvm/gems/ruby-1.9.3-p0@devfun/gems/guard-rspec-0.6.0/lib/guard/rspec/formatters/notification_rspec.rb", "spec"]...
No examples found.
Finished in 0.00175 seconds
0 examples, 0 failures
Done.
無事にSporkを利用してRSpecが動き、Growlに通知されましたね。
それでは、構築したTDD環境で開発を始めようか!