2011-01-24 63 views
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的外部應用程序。當我從我的引擎複製測試並運行它們時,它們都會通過。

有什麼我失蹤?也許有更好的方法來編寫測試引擎?

回答

3

這真是一種怪誕的恥辱,我忘了將設計初始值設定項添加到虛擬應用程序中。不知何故,我想到了我的外部應用程序。通過初始化器,它可以完美地工作。

+0

好:)我將使用它作爲我的寶石指南:) – m4risU 2011-01-25 00:07:40

相關問題