ホーム > Linux, Ruby, Ubuntu > Ubuntu 11.10 Rails高速BDD環境を作る。

Ubuntu 11.10 Rails高速BDD環境を作る。

今回は、前回のRVMからインストールする場合の環境 に、高速BDD(振る舞い駆動開発)環境を作成します。

以下の手順で進めます。

  1. 高速BDD(振る舞い駆動開発)環境の作成
  2. 高速BDD環境の動作確認と速度比較
  3. ファイルが更新されたタイミングで自動テスト
  4. おまけ(Spork起動時のエラーについて)

今回のインストール環境および補足説明

  • Ubuntu 11.10 デスクトップ版にインストールします。
  • Ruby on Rails 3.0 を使用します。設定等については、前回の記事 をご覧ください。
  • 記事中では、ホームに ~/work/sample/ という名前の Rails アプリケーション環境を作成し使用しています。



1. 高速BDD(振る舞い駆動開発)環境の作成

#Railsアプリケーションフォルダに移動
$ cd ~/work/sample/

#Gemfileの編集
$ vi Gemfile
後述の Gemfileの編集内容 を書き込んで、保存します。

#Bundlerからインストール
$ bundle install

#RSpecとCucumberの設定
$ rails generate rspec:install
$ rails generate cucumber:install

#Sporkの設定
$ spork --bootstrap
$ vi spec/spec_helper.rb
後述の spec_helper.rbの編集内容 を書き込んで、保存します。

$ spork cucumber --bootstrap
$ vi features/support/env.rb
後述の env.rbの編集内容 を書き込んで、保存します。

(2011/12/09 追記)
  Cucumberのbootstrapを追加。
 

Gemfileの編集内容

source 'http://rubygems.org'
gem 'rails', '3.0.10'
gem 'sqlite3'

group :development, :test do
  gem 'webrat'
  gem 'rspec'
  gem 'rspec-rails'
  gem 'cucumber'
  gem 'cucumber-rails'
  gem 'database_cleaner'

  # sporkを使う場合
  gem 'spork', '~> 0.9.0.rc'

  # guardを使う場合
  gem 'rb-fsevent'
  gem 'guard-spork'
  gem 'guard-rspec'
  gem 'guard-cucumber'

  # notifyを使う場合(Ubuntu)
  gem 'libnotify'
  gem 'rb-inotify'
end

(2011/12/09 追記)
  Sporkのバージョン指定を変更。後述の4-1の対応は不要になります。
 

spec_helper.rbの編集内容
(Spork.preforkの中にRSpecの記述を移動しています。)

require 'rubygems'
require 'spork'

Spork.prefork do

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
  config.mock_with :rspec

  # 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 version of
  # rspec-rails.
  config.infer_base_class_for_anonymouse_controllers = false
end

Spork.each_run do

end

env.rbの編集内容
(Spork.preforkの中にCucumberの記述を移動しています。)

require 'rubygems'
require 'spork'

Spork.prefork do

  require 'cucumber/rails'

  Capybara.default_selector = :css

  ActionController::Base.allow_rescue = false

  begin
    DatabaseCleaner.strategy = :transaction
  rescue NameError
    raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
  end

  Cucumber::Rails::Database.javascript_strategy = :truncation

end

Spork.each_run do

end

 


2. 高速BDD環境の動作確認と速度比較

#確認用にモデルを追加
$ rails generate model testuser name:string email:string
$ rake db:migrate
$ vi spec/model/testuser_spec.rb
  pendingを削除し、テストを記述します。
  今回は速度比較を行う目的だけなので、簡単に通るテストを書きました。
  require 'spec_helper'
  describe Testuser do
    it '1+1=2' do
      (1+1).should == 2
    end
  end

#アプリケーションの起動
$ rails server

#別の端末を開きSporkを起動します。
$ cd ~/work/sample/
$ spork
~(途中省略)
Loading Spork.prefork block...
Spork is ready and listening on 8989!

#さらに別の端末を開き、実行して確認します。
$ cd ~/work/sample/

  #Sporkなしで実行した場合
  $ time rspec spec/
    Finished in 6.24 seconds
    1 example, 0 failures

    real  0m6.387s
    user  0m5.896s
    sys   0m0.472s
      約6秒ほど。普段はこんな感じです。

  #Sporkありで実行した場合(drbオプション付きで実行します。)
  $ time rspec --drb spec/
    Finished in 156.89 seconds
    1 example, 0 failures

    real  0m0.448s
    user  0m0.164s
    sys   0m0.060s
      1秒未満。かなり早いです。

#Rails、Sporkを停止します。
  それぞれの端末で Ctrl+C を押し、実行中のプログラムを停止します。

 


3. ファイルが更新されたタイミングで自動テスト

Guard を使って、ファイルが更新されたタイミングで自動テストが行われるようにします。

#RSpec実行時のオプションを指定します。(Sporkが使われるように設定)
$ vi .rspec
  次の内容を書き込んで、保存します。
  --colour
  --drb

#Guardの初期設定を行います。
$ guard init spork
$ guard init rspec
$ guard init cucumber

#Guardを起動します。
$ guard start

※まとめ記事にGuardfile の編集内容を追記しました。
  Ubuntu 11.10 Rails高速BDD環境を作る。(まとめ)
 

notifyがテストの状態を通知してくれます。
  

  
  Guardから通知されるRSpecの秒数が、Guard起動時からの経過時間になるのはなぜだろう?
 


4. おまけ(Spork起動時のエラーについて)

Spork起動時に以下の様なエラーが起きる場合の対処方法

ERROR: Could not start Spork server for RSpec. Make sure you can use it manually first.

こちらのサイトを参考にさせていただきました。

Deep valley – rspec + guard + spork で高速な自動テスト実行環境構築
  http://kitbc.s41.xrea.com/main/?use_guard_spork

 
1.Sporkのバージョンを変更する

$ vi Gemfile
gem 'spork', '=0.9.0.rc9'
$ bundle update spork

2.guardのタイムアウトについて
  私の環境でも同様のワーニングが出ていましたが、
  Sporkのバージョンを上げるとワーニングがでなくなりました。
 
 
(2011/12/13 追記)
  Ubuntu 11.10 Rails高速BDD環境を作る。(まとめ)
 

カテゴリー:Linux, Ruby, Ubuntu タグ: ,

コメントを残す