- 公開日:2016年05月16日
- 最終更新日:2016年05月20日
記事概要
RailsエンジンでRspecを利用する方法をまとめた記事です。
環境
- rbenv
- bundler
- rails 4.2.5
- ruby 2.3.0
Rspecを利用するエンジンプロジェクトの作成
RailsエンジンでRspecを利用する時は、以下のコマンドでサブプロジェクトを作成します。
// rspecを使う場合 bundle exec rails plugin new user_engine --mountable -T --dummy-path=spec/dummy
--dummy-pathオプションで、spec/dummyを指定します。指定しない場合は、test/dummyになります。
すでに作成済みのサブプロジェクトの場合は、フォルダ名を
test/dummy → spec/dummy
に変更します。
Rspecのインストール
specフォルダが作成できたら、Rspecをインストールします。
必要なGemをGemfileに記述します。
group :test do gem 'rspec-rails' gem "factory_girl_rails", ">= 4.1.0" gem 'database_cleaner', ['>= 1.2', '!= 1.4.0', '!= 1.5.0'] gem 'rubocop', require: false end
bundlerでGemをインストールします。
cd /workspace/sub_project bundle install
サブプロジェクトにRspecをインストールします。
cd /workspace/sub_project
rbenv exec bundle exec rails generate rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb
specの動作に必要なspecファイルが作成されます。
設定ファイル編集
インストールしたspecファイルに、specの動作に必要な設定をします。
rails_helper.rbとengine.rbを編集します。
rails_helper.rb編集
まずrails_helper.rbを編集します。
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path("../dummy/config/environment", __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'factory_girl_rails'
Dir["#{File.dirname(__FILE__)}/spec/factories/**/*.rb"].each { |f| require f }
RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
  end
  # every time
  config.before(:each) do
    DatabaseCleaner.start
  end
  # every time
  config.after(:each) do
    DatabaseCleaner.clean
  end
end
重要なポイントは以下となります。
environmentの設定
環境設定の読み込み位置を変更します。
../../config/environment → ../dummy/config/environment
factory_girl_railsの設定
require 'factory_girl_rails'
Dir["#{File.dirname(__FILE__)}/spec/factories/**/*.rb"].each { |f| require f }
factory_girlは、DBのテストが必要な場合には導入しておきましょう。
database_cleanerの設定
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
  end
  # every time
  config.before(:each) do
    DatabaseCleaner.start
  end
  # every time
  config.after(:each) do
    DatabaseCleaner.clean
  end
factory_girlを利用する場合は、database_cleanerも導入することをお勧めします。
engine.rb編集
次にengine.rbを編集します。
module UserEngine
  class Engine < ::Rails::Engine
    isolate_namespace UserEngine
    config.generators do |g|
      g.test_framework :rspec, :fixture => false
      g.fixture_replacement :factory_girl, :dir => 'spec/factories'
    end
    initializer "model_core.factories", :after => "factory_girl.set_factory_paths" do
      FactoryGirl.definition_file_paths << File.expand_path('../../../spec/factories', __FILE__) if defined?(FactoryGirl)
    end
  end
end
lib/{engine_name}/engine.rbファイルは、Railsアプリケーションが標準で持つconfig/application.rbファイルと同一の機能を持ちます。
このファイルにもspecファイルの設定を記述します。
Rspecの実行
全ての設定を終えたらRspecの実行を行います。
cd /workspace/sub_project bundle exec rspec spec 52 examples, 0 failures, 16 pending
成功しました。
まとめ
通常のRailsでRspecを利用している場合は、RailsエンジンでもRspecを使うことをお勧めします。
設定ファイル等のわずかな違いに、多少つまづくことがあるかもしれませんが、良い学習の機会と捉え、Rubyの力の底上げを図りましょう。
以上。

 
 
0 件のコメント:
コメントを投稿