简体   繁体   English

设计和Rspec测试集成

[英]Devise & Rspec Test Integration

I'm trying to run tests with Devise and Rspec and have been unsuccessful thus far - any help would be greatly appreciated. 我正在尝试使用Devise和Rspec进行测试并且到目前为止都没有成功 - 任何帮助都将非常感激。

rake spec:requests --trace

gives me this error 给了我这个错误

`/Users/carl/.rvm/rubies/ruby-1.9.2-head/bin/ruby -S rspec ./spec/requests/users_spec.rb
/Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:503:in ‘load_missing_constant': Expected /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise_controller.rb to define DeviseController (LoadError)
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in ‘block in const_missing'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘const_missing'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise/sessions_controller.rb:1:in ‘<top (required)>'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘block in require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in ‘load_dependency'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in ‘require_or_load'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in ‘load_missing_constant'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in ‘block in const_missing'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘const_missing'
    from /Users/carl/rails_projects/my_rails_app_name/app/controllers/application_controller.rb:14:in ‘<class:ApplicationController>'
    from /Users/carl/rails_projects/my_rails_app_name/app/controllers/application_controller.rb:1:in ‘<top (required)>'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘block in require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in ‘load_dependency'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in ‘require_or_load'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in ‘load_missing_constant'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in ‘block in const_missing'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘const_missing'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:229:in ‘block in constantize'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in ‘constantize'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/core_ext/string/inflections.rb:54:in ‘constantize'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise_controller.rb:2:in ‘<top (required)>'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘block in require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in ‘load_dependency'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in ‘require_or_load'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in ‘load_missing_constant'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in ‘block in const_missing'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in ‘const_missing'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise/confirmations_controller.rb:1:in ‘<top (required)>'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘block in require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in ‘load_dependency'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in ‘require'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in ‘require_or_load'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:313:in ‘depend_on'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:225:in ‘require_dependency'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:439:in ‘block (2 levels) in eager_load!'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:438:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:438:in ‘block in eager_load!'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:436:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:436:in ‘eager_load!'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/application/railties.rb:8:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/application/railties.rb:8:in ‘all'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/engine.rb:434:in ‘eager_load!'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/application/finisher.rb:53:in ‘block in <module:Finisher>'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:30:in ‘instance_exec'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:30:in ‘run'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:55:in ‘block in run_initializers'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:54:in ‘each'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/initializable.rb:54:in ‘run_initializers'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/application.rb:136:in ‘initialize!'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in ‘method_missing'
    from /Users/carl/rails_projects/my_rails_app_name/config/environment.rb:5:in ‘<top (required)>'
    from /Users/carl/rails_projects/my_rails_app_name/spec/spec_helper.rb:3:in ‘require'
    from /Users/carl/rails_projects/my_rails_app_name/spec/spec_helper.rb:3:in ‘<top (required)>'
    from /Users/carl/rails_projects/my_rails_app_name/spec/requests/users_spec.rb:1:in ‘require'
    from /Users/carl/rails_projects/my_rails_app_name/spec/requests/users_spec.rb:1:in ‘<top (required)>'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in ‘load'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in ‘block in load_spec_files'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in ‘map'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in ‘load_spec_files'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in ‘run'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in ‘run'
    from /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in ‘block in autorun'
rake aborted!
/Users/carl/.rvm/rubies/ruby-1.9.2-head/bin/ruby -S rspec ./spec/requests/users_spec.rb failed
/Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/rake_task.rb:137:in ‘block (2 levels) in initialize'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/file_utils_ext.rb:60:in ‘verbose'
/Users/carl/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.11.1/lib/rspec/core/rake_task.rb:127:in ‘block in initialize'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in ‘call'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in ‘block in execute'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in ‘each'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in ‘execute'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in ‘block in invoke_with_call_chain'
/Users/carl/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/monitor.rb:201:in ‘mon_synchronize'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in ‘invoke_with_call_chain'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in ‘invoke'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in ‘invoke_task'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in ‘block (2 levels) in top_level'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in ‘each'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in ‘block in top_level'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in ‘standard_exception_handling'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in ‘top_level'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in ‘block in run'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in ‘standard_exception_handling'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in ‘run'
/Users/carl/.rvm/gems/ruby-1.9.2-head@global/gems/rake-0.9.2.2/bin/rake:33:in ‘<top (required)>'
/Users/carl/.rvm/gems/ruby-1.9.2-head/bin/rake:19:in ‘load'
/Users/carl/.rvm/gems/ruby-1.9.2-head/bin/rake:19:in ‘<main>'
`

Gemfile for :development, :test gem 'sqlite3' gem 'rspec-rails' gem 'capybara' 为的Gemfile:发展:测试gem 'sqlite3' gem 'rspec-rails' gem 'capybara'

spec/requests/user_spec.rb 规格/请求/ user_spec.rb

> `require 'spec_helper'
> 
> describe "Users" do
>   describe "GET /" do
>     it "goes to homepage" do
>       visit '/'
>       response.status.should be(200)
>     end   
>   end 
> end`

spec/spec_helper.rb 投机/ 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'
#require 'capybara/rails'


# Note: To create integration test: $rails g integration_test blah --integration-tool=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}

RSpec.configure do |config|

  # Include devise in Rspec tests
  config.include Devise::TestHelpers, :type => :controller

  # ## 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"
end

I don't know exactly what is going wrong, but a quick search for LoadError and the "Expected ... to define" pattern brought up a few relevant discussions. 我不确切地知道出了什么问题,但快速搜索LoadError和“预期......定义”模式引发了一些相关的讨论。

From the first link: 从第一个链接:

The error message is misleading. 错误消息具有误导性。 Expected foo.rb to define Foo usually just means that something bad happened when loading foo.rb (syntax errors, missing constant etc...). 期望foo.rb来定义Foo通常只是意味着在加载foo.rb(语法错误,缺少常量等等)时发生了一些不好的事情。 You can usually get a better error message by just typing Foo into script/console 只需在脚本/控制台中键入Foo,通常可以获得更好的错误消息

The error is coming directly from Devise, so perhaps there is some naming conflict in your controllers with the devise controllers? 错误直接来自Devise,所以也许你的控制器与设计控制器有一些命名冲突? I don't use devise so I don't really know much about how it works. 我不使用设计,所以我真的不太了解它是如何工作的。 Although I doubt that it's the problem, have a look at the file /Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise_controller.rb and make sure it's not corrupted. 虽然我怀疑这是问题,但请查看文件/Users/carl/.rvm/gems/ruby-1.9.2-head/gems/devise-2.1.2/app/controllers/devise_controller.rb并确保它没有被破坏。 You might try to reinstall the gem in rvm. 您可以尝试在rvm中重新安装gem。

A friend helped me figure this one out. 一位朋友帮我解决了这个问题。 In my application controller I was overriding Devise with the below code. 在我的应用程序控制器中,我用下面的代码覆盖了Devise。 Commenting this out allowed rake spec:requests to pass successfully. 评论这个允许rake spec:requests成功通过。

# ===============================================
  # == Commented this out to make rspec tests work
  # ===============================================
  # Override Devise create session controller
  # Devise::SessionsController.class_eval do
    # def create
    #  resource = warden.authenticate!(auth_options)
    #     set_flash_message(:notice, :signed_in) if is_navigational_format?
    #     sign_in(resource_name, resource)
    #     @sign_in_path = after_sign_in_path_for(resource)
    #  end
    # end

Thank you again for your time and help! 再次感谢您的时间和帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM