Как протестировать движок Rails3 с помощью Devise и rspec

Я разрабатываю движок Rails, который зависит от Devise 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, я мог писать тесты и запускать их с помощью

rake spec

Теперь у меня есть жемчужина Devise и такая модель пользователя

class Manager::User < ActiveRecord::Base
    devise :database_authenticatable, :registerable, # ...
end

и мои тесты не указывают на класс пользователя и вызов devise. Вспомогательная функция spec выглядит так

# 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

the 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!

the application.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)

the 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)

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 и все драгоценные камни загружены (выполняется Bundler.setup и доступна константа Devise).
Теперь я создал внешнее приложение, использующее гем my_engine. Когда я копирую тесты из своего движка и запускаю их, все они проходят.

Что-то мне не хватает? Может, есть лучший способ написать тесты для движка?

8
задан tshepang 18 October 2013 в 14:09
поделиться