LoginSignup
24
23

More than 5 years have passed since last update.

RSpec, Capybara, factory_girl, Spork, GuardでGrowlなRailsテスト環境を作る

Last updated at Posted at 2012-06-10

環境

  • Growl 1.4
  • GrowlNotify 1.3
  • Ruby on Rails 3.2.5
  • Capybara 1.1.2
  • capybara-webkit 0.12.1
  • factory_girl_rails 3.3.0
  • guard-rspec 1.0.0
  • guard-spork 1.0.0
  • rspec-rails 2.10.1
  • spork-rails 3.2.0

手順

Growlの設定

GrowlをMac App Storeでインストールしたあと、以下のURLから「GrowlNotify」をインストールします。これをインストールしないとGuard経由でGrowlの通知が表示されません。

http://growl.info/downloads

RSpec, Sporkの設定

Gemfileに以下を追記します。

group :development, :test do
  gem 'capybara', '1.1.2'
  gem 'capybara-webkit', '0.12.1'
  gem 'factory_girl_rails', '3.3.0'
  gem 'rspec-rails', '2.10.1'
  gem 'spork-rails', '3.2.0'
end

group :development do
  gem 'growl', '1.0.3'
  gem 'guard-rspec', '1.0.0'
  gem 'guard-spork', '1.0.0'
end

$ bundle install 後、以下のコマンドを実行して spec/spec_helper.rb を生成します。

$ bundle exec rails generate rspec:install

生成された spec/spec_helper.rb を以下のように修正します。

# coding: utf-8

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV['RAILS_ENV'] ||= 'test'
  require File.expand_path('../../config/environment', __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'

  # 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 }

  # capybara-webkit をインストールしていると :webkit が指定できるようになります。
  Capybara.javascript_driver = :webkit

  RSpec.configure do |config|
    # 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

    # Deviseを使用している場合は以下を追記します。
    # https://github.com/plataformatec/devise#test-helpers
    config.include Devise::TestHelpers, type: :controller
  end
end

Spork.each_run do
  # factory_girlのfactoriesに変更があったとき更新するようにします。
  FactoryGirl.reload
end

Guardの設定

以下を実行して Guardfile を生成します。

$ bundle exec guard init spork
$ bundle exec guard init rspec

生成された Guardfile を以下のように修正します。

# coding: utf-8

# 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(%r{^config/environments/.+\.rb$})
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb') { :rspec }
end

# "--drb" オプションを付加して、Spork側でRSpecが実行されるようにします。
# "all_after_pass" と "all_on_start" をそれぞれfalseにして、
# テストコードを修正するたびにテスト全体を実行しないようにします。
guard 'rspec', cli: '--drb', all_after_pass: false, all_on_start: false 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" }
  # 個人的にSlimを使用しているので、Slimの拡張子も監視するよう追記しています。
  watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
    [
      "spec/routing/#{m[1]}_routing_spec.rb",
      "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
      "spec/acceptance/#{m[1]}_spec.rb"
    ]
  end
  watch(%r{^spec/support/(.+)\.rb$}) { 'spec' }
  watch('config/routes.rb') { 'spec/routing' }
  watch('app/controllers/application_controller.rb') { 'spec/controllers' }
  # Capybara request specs
  # ここでもSlimを監視するよう追記しています。
  watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end

これで以下のコマンドを実行すると、テストコードが記述されたファイルを保存するたびにそのファイル内のテストが実行されるようになり、テスト結果がGrowlで通知されるようになります。

$ bundle exec guard

おしまい。

24
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23