Devise Gem に依存する Rails エンジンを開発しています。私のエンジン自体も宝石であり、そのためのテストを書く必要があります。このために、gem の内部からエンジンをテストする方法をたくさん読んだ結果、次の設定にたどり着きました。
my_engine
+- app # my engine stuff
+- config # my engine stuff
+- Gemfile
+- lib # my engine stuff
+- public # my engine stuff
+- spec
+- controllers # controller tests
+- models # model tests
+- dummy # a dummy application that is using the 'my_engine/Gemfile'
+- spec_helper.rb # the spec helper that boots the dummy app
Devise gem を含める前に、テストを作成して実行することができました
rake spec
今、私はDevise gemとこのようなユーザーモデルを持っています
class Manager::User < ActiveRecord::Base
devise :database_authenticatable, :registerable, # ...
end
私のテストは、ユーザークラスとデバイス呼び出しを指して失敗します。
`method_missing': undefined method `devise'
だから今、私はエラーを調査し、デバイスと私のエンジンを一緒にダミーアプリケーションを起動する方法を見つけようとしています. 仕様ヘルパーは次のようになります
# my_engine/spec/spec_helper.rb#
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../dummy/config/environment.rb", __FILE__) # boots the dummy app and fails
# more rspec stuff gies here
evnironment.rb
# my_engine/spec/dummy/config/environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Dummy::Application.initialize!
アプリケーション.rb
# my_engine/spec/dummy/config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env) if defined?(Bundler)
# Dummy::Application definition follows here (the common stuff)
boot.rb
# my_engine/spec/dummy/config/boot.rb
require 'rubygems'
# Loads the gemfile from 'my_engine/Gemfile'
gemfile = File.expand_path('../../../../Gemfile', __FILE__)
begin
ENV['BUNDLE_GEMFILE'] = gemfile
require 'bundler'
Bundler.setup
rescue Bundler::GemNotFound => e
STDERR.puts e.message
STDERR.puts "Try running `bundle install`."
exit!
end if File.exist?(gemfile)
宝石ファイル
# my_engine/Gemfile
source :gemcutter
gem 'rails', '3.0.3'
gem 'rake'
gem 'devise', '>=1.2.rc'
gem 'declarative_authorization', '0.5.2'
group :test do
gem "cucumber"
gem "cucumber-rails"
gem "database_cleaner"
gem "capybara", ">= 0.4.0"
gem "capybara-envjs"
gem "culerity"
gem "webrat"
gem "rspec-rails", ">= 2.4.0"
gem "jeweler"
end
私もこのように私のエンジンを要求しようとしました
gem "my_engine", :path => "./"
Gemfile と gem がすべて読み込まれます (Bundler.setup が実行され、Devise 定数が使用可能になります)。
my_engine gem を使用する外部アプリケーションを作成しました。エンジンからテストをコピーして実行すると、すべて合格します。
不足しているものはありますか?エンジンのテストを作成するためのより良い方法はありますか?