8
我正在开发一个依赖于Devise Gem的Rails引擎。我的引擎本身也是一个宝石,我需要为这件事写测试。为此,我做了很多如何从宝石的内部测试发动机的读数和我来到了以下设置:如何使用Devise和rspec来测试Rails3引擎
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
之前,我包括在设计创业板,我可以写测试,并与
运行它们rake spec
现在我有设计的宝石和用户模型像这样
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!
的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)
的的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贯穿并制定常数可用)。
现在我创建了一个使用my_engine gem的外部应用程序。当我从我的引擎复制测试并运行它们时,它们都会通过。
有什么我失踪?也许有更好的方法来编写测试引擎?
好:)我将使用它作为我的宝石指南:) – m4risU 2011-01-25 00:07:40