我有一种我想测试的控制器方法。我有一个需要用户登录的案例。我为此目的使用Authlogic,所以我用这样的方式写了我的测试:
test "should get edit" do
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
# line above was required by line below, but honestly I don't know why
UserSession.new({email: 'my.email@gmail.com', password: 'aaaabbbb', password_confirmation: 'aaaabbbb'})
get edit_service_url(@service)
assert_response :success
end
测试失败,看起来我还没有登录。我想确保我的会话没有创建,所以我添加了简单的puts
行:
test "should get edit" do
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
UserSession.new({email: 'my.email@gmail.com', password: 'aaaabbbb', password_confirmation: 'aaaabbbb'})
puts UserSession.find
get edit_service_url(@service)
assert_response :success
end
但是在添加它之后我得到了这个错误,这对我来说是完全不可理解的:
错误:ServicesControllerTest#test_should_get_edit:NoMethodError: 未定义的方法' params' for#/home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/testing/assertions/routing.rb:175:in ' method_missing的' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/testing/integration.rb:491:in ' method_missing的' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/authlogic-3.6.1/lib/authlogic/controller_adapters/abstract_adapter.rb:30:in ' PARAMS' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/authlogic-3.6.1/lib/authlogic/session/params.rb:111:in ' params_credentials' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/authlogic-3.6.1/lib/authlogic/session/params.rb:86:in ' params_enabled&#39?; /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/authlogic-3.6.1/lib/authlogic/session/params.rb:80:in ' persist_by_params' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:382:in '阻止make_lambda' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:169:in '阻止(2级)停止' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/authlogic-3.6.1/lib/authlogic/session/callbacks.rb:68:in '阻止包含' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:170:in 阻止停止' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:454:in '阻止通话' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:454:in '每个' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:454:in '调用' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:101:in ' __ run_callbacks __' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:750:in ' _run_persist_callbacks' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.6/lib/active_support/callbacks.rb:90:in ' run_callbacks' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/authlogic-3.6.1/lib/authlogic/session/callbacks.rb:93:in '坚持' /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/authlogic-3.6.1/lib/authlogic/session/persistence.rb:56:in '坚持&#39?; /home/karol/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/authlogic-3.6.1/lib/authlogic/session/persistence.rb:39:in '发现' /home/karol/GitProjects/spotfinder/test/controllers/services_controller_test.rb:52:in '阻止'
它是由什么引起的?如何正确地编写我的测试?
修改
我正在测试的代码:
class ServicesController < ApplicationController
before_action :authorise_for_service_id, only: [:edit]
# ...
def edit
end
# ...
private:
# ...
def authorise_for_service_id
if !is_user_id_current?(Service.find(params[:id]).user_id)
redirect_to service_path(params[:id]), notice: 'Cannot perform your action: access denied.'
end
end
end
module ApplicationHelper
# ...
def is_user_id_current?(user_id)
return current_user && current_user.id == user_id
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
# ...
end
class UserSessionsController < ApplicationController
def new
@user_session = UserSession.new
respond_to do |format|
format.html # new.html.erb
end
end
# POST /user_sessions
def create
@user_session = UserSession.new(user_session_params)
respond_to do |format|
if @user_session.save
format.html { redirect_to session[:return_to] || root_url, :notice => 'Login Successful!' }
else
format.html { render :action => "new" }
end
end
end
# DELETE /user_sessions/1
def destroy
@user_session = UserSession.find
@user_session.destroy
respond_to do |format|
format.html { redirect_to session[:return_to] || root_url, :notice => 'Goodbye!' }
end
end
private
def user_session_params
params.require(:user_session).permit(:email, :password)
end
end