21
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RSpec + Spork + FactoryGirl + Guard によるテスト環境構築

Last updated at Posted at 2013-05-27

アプリケーションを作成。

rails new test_env
cd test_env

Gemfileに以下を追記。

Gemfile
group :test do
  gem 'spork'
  gem 'guard-spork'
end

group :development, :test do
  gem 'rspec-rails'
  gem 'guard'
  gem 'guard-rspec'
  gem 'factory_girl_rails'
end

インストール。

bundle install

.rspecspec/spec_helper.rbを生成する。

rails generate rspec:install

Sporkを使用するように、.rspecに以下を追記。

.rspec
--drb

動作確認用に、scaffoldでUserを生成。

rails generate scaffold user name:string age:integer

マイグレーション。

rake db:migrate

以下のようにして、spec/spec_helper.rbに、spork用のエントリを追加。

spork --bootstrap

FactoryGirlの変更を反映させるため、spec/spec_helper.rbに以下のように追記。

spec/spec_helper.rb
Spork.each_run do
  # This code will be run each time you run your specs.
  FactoryGirl.reload        ### added
end

Guardfileを生成。

guard init spork
guard init rspec

spec/factories/以下の変更に対して対応するテストが行われるように、Guardfileに以下のように追記。

Guardfile
require 'active_support/inflector'    ### added

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

(中略)

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }

  watch(%r{^spec/factories/(.+)\.rb$}) do |m|        ### added
    %W[                                              ### added
      spec/models/#{m[1].singularize}_spec.rb        ### added
      spec/controllers/#{m[1]}_controller_spec.rb    ### aaded
    ]                                                ### added
  end                                               ### added
end

テスト用DBの準備。

rake db:test:prepare

以上で、コードの変更に対して即座にテストが走る環境になる。

21
21
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
21
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?