2013-09-05 34 views
0

我一直在試圖使用RSpec與Rails 4,但遇到了這個錯誤,我無法擺脫。Rails 4,RSpec應用程序已經初始化

但是,當我在單個* .rb文件上運行測試時,它的工作原理沒有任何錯誤。我使用警衛並嘗試在單一模式下運行rspec,但收到相同的錯誤。

13:46:26 - INFO - Running: spec 
/Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:214:in `initialize!': Application has been already initialized. (RuntimeError) 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing' 
    from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/config/environment.rb:5:in `<top (required)>' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require' 
    from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/spec/spec_helper.rb:3:in `<top (required)>' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require' 
    from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/spec/requests/products_spec.rb:1:in `<top (required)>' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `block in load' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `each' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `load_spec_files' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:22:in `run' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run' 
    from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun' 

這是我spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 
require 'capybara/rspec' 

# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 

# Checks for pending migrations before tests are run. 
# If you are not using ActiveRecord, you can remove this line. 
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) 

RSpec.configure do |config| 
    # ## Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    # config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    # Run specs in random order to surface order dependencies. If you find an 
    # order dependency and want to debug it, you can fix the order by providing 
    # the seed, which is printed after each run. 
    #  --seed 1234 
    config.order = "random" 

    config.include Devise::TestHelpers, :type => :controller 
end 

class ActionController::TestCase 
    include Devise::TestHelpers 
end 
+0

請添加您用於運行所有測試的命令 - 例如''rake spec''或'rake'。 :) –

+0

嘗試刪除'config.include Devise :: TestHelpers,:type =>:controller'並查看發生了什麼。可能沒有解決,但可能會發現。 –

+0

我使用警衛來運行所有測試=) – Philip

回答

1

試試你spec_helper設置這樣的:

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 
require 'capybara/rspec' # NOTE: I DON'T have this but you do... 
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 
RSpec.configure do |config| 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 
    config.use_transactional_fixtures = true 
    config.infer_base_class_for_anonymous_controllers = false 
end 
1

我不知道,如果你已經得到了解決,但在這裏我去無論如何:

自從昨天起我面臨同樣的問題。我發現按時間運行一個 單個規格,或者從所有規格中刪除'require'spec_helper「,但是其中一個, 它可以工作。 而幾分鐘前,我發現問題是ruby版本。

如果您將ruby-2.0.0-p0升級到ruby-2.0.0-p247,那麼問題就會消失!

我希望我一直有幫助。

相關問題