Ruby on rails 功能测试持续失败(Rspec、DEVICE)
我正在尝试实现一个rspec功能测试,以检查用户是否使用designe登录。我已经一次又一次地检查了我的代码,并尝试了不同的解决方案,但到目前为止没有任何效果 我已经包括了我认为相关的文件。如果缺少什么,请告诉我。欢迎提出任何建议 等级库/rails\u helper.rbRuby on rails 功能测试持续失败(Rspec、DEVICE),ruby-on-rails,ruby,rspec,devise,Ruby On Rails,Ruby,Rspec,Devise,我正在尝试实现一个rspec功能测试,以检查用户是否使用designe登录。我已经一次又一次地检查了我的代码,并尝试了不同的解决方案,但到目前为止没有任何效果 我已经包括了我认为相关的文件。如果缺少什么,请告诉我。欢迎提出任何建议 等级库/rails\u helper.rb # This file is copied to spec/ when you run 'rails generate rspec:install' ENV['RAILS_ENV'] ||= 'test' require F
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
# Add additional requires below this line. Rails is not loaded until this point!
require 'spec_helper'
require 'rspec/rails'
# note: require 'devise' after require 'rspec/rails'
require 'devise'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# 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
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
#Added for Devise
config.include Devise::Test::ControllerHelpers, :type => :controller
end
等级库/控制器/用户\控制器\等级库rb
require 'rails_helper'
describe UsersController, :type => :controller do
before do
@user = User.create!(email: 'example@example.com', password: 'example')
end
# let(:user) { User.create!(email: 'example@example.com', password: 'example') }
describe 'GET #show' do
context "when user is logged in" do
before do
sign_in @user
end
it "loads correct user details" do
get :show, id: @user.id
expect(response).to have_http_status(200)
expect(assigns(:user)).to eq @user
end
end
context "when no user is logged in" do
it "redirects to login" do
get :show, id: @user.id
expect(response).to redirect_to(root_path)
end
end
end
end
以及我在运行bundle exec rpsec后从终端收到的反馈
Failures:
1) UsersController GET #show when user is logged in loads correct user details
Failure/Error: elsif user?
NoMethodError:
undefined method `user?' for #<Ability:0x007fa1d784cb98>
# ./app/models/ability.rb:9:in `initialize'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:361:in `new'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:361:in `current_ability'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:342:in `authorize!'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:49:in `authorize_resource'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:34:in `load_and_authorize_resource'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:10:in `block in add_before_action'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/rails-controller-testing-1.0.1/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:33:in `block in process'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:100:in `catch'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:100:in `_catch_warden'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:33:in `process'
# /Users/Admin/.rvm/gems/ruby-2.3.0/gems/rails-controller-testing-1.0.1/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/users_controller_spec.rb:19:in `block (4 levels) in <top (required)>'
2) UsersController GET #show when no user is logged in redirects to login
Failure/Error: expect(response).to redirect_to(root_path)
Expected response to be a redirect to <http://test.host/> but was a redirect to <http://test.host/users/sign_in>.
Expected "http://test.host/" to be === "http://test.host/users/sign_in".
# ./spec/controllers/users_controller_spec.rb:29:in `block (4 levels) in <top (required)>'
Finished in 0.90214 seconds (files took 11.05 seconds to load)
6 examples, 2 failures
Failed examples:
rspec ./spec/controllers/users_controller_spec.rb:18 # UsersController GET #show when user is logged in loads correct user details
rspec ./spec/controllers/users_controller_spec.rb:27 # UsersController GET #show when no user is logged in redirects to login
故障:
1) UsersController在用户登录时获取#显示加载正确的用户详细信息
失败/错误:elsif用户?
命名错误:
未定义的方法'user'#
#./app/models/ability.rb:9:in'initialize'
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller\u additions.rb:361:in'new'
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller\u additions.rb:361:在“当前能力”中
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller\u additions.rb:342:in“authorize!”
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller\u resource.rb:49:in“authorize\u resource”
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller\u resource.rb:34:in'load\u and\u authorize\u resource'
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/cancancan-1.15.0/lib/cancan/controller\u resource.rb:10:在“操作前添加块”中
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/rails-controller-testing-1.0.1/lib/rails/controller/testing/template_assertions.rb:61:在“进程”中
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/designe-4.2.0/lib/designe/test/controller\u helpers.rb:33:在“进程中的块”中
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/designe-4.2.0/lib/designe/test/controller\u helpers.rb:100:in'catch'
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/designe-4.2.0/lib/designe/test/controller\u helpers.rb:100:in`\u catch\u warden'
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/designe-4.2.0/lib/designe/test/controller\u helpers.rb:33:正在进行中
#/Users/Admin/.rvm/gems/ruby-2.3.0/gems/rails-controller-testing-1.0.1/lib/rails/controller/testing/integration.rb:12:in'block(2层)in'
#./spec/controllers/users\u controllers\u spec.rb:19:in'block(4级)in'
2) UsersController GET#在没有用户登录时显示重定向到登录
失败/错误:预期(响应)。将\u重定向到(根路径)
预期响应为重定向到,但为重定向到。
预期的”http://test.host/“未来==”http://test.host/users/sign_in".
#./spec/controllers/users\u controller\u spec.rb:29:in'block(4级)in'
完成时间为0.90214秒(文件加载时间为11.05秒)
6例,2次失败
失败的示例:
rspec./spec/controllers/users\u controller\u spec.rb:18#users controller GET#用户登录时显示正确的用户详细信息
rspec./spec/controllers/users#controller#spec.rb:27#users controller GET#在没有用户登录时显示重定向到登录
一个朋友实际上帮我弄明白了。显然,有一些问题涉及到单独的宝石坎坎坎。我必须进入models/ability.rb文件并更改其中的一行
在原始代码中,如果,则显示该行!user.admin?
只是用户?
所以,如果你遇到同样的问题,那就是解决办法
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new #guest user (not logged in)
# can :manage, User, id: user.id
if user.admin?
can :manage, :all
elsif !user.admin?
can :manage, User, id: user.id
else
can :read, :all
end
end
end
实际上是一个朋友帮我弄明白的。显然,有一些问题涉及到单独的宝石坎坎坎。我必须进入models/ability.rb文件并更改其中的一行 在原始代码中,如果
,则显示该行!user.admin?
只是用户?
所以,如果你遇到同样的问题,那就是解决办法
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new #guest user (not logged in)
# can :manage, User, id: user.id
if user.admin?
can :manage, :all
elsif !user.admin?
can :manage, User, id: user.id
else
can :read, :all
end
end
end
检查这个:谢谢!事实上,我有一个朋友帮我解决了这个问题!事实上,我有一个朋友帮我解决了这个问题。