我已经通过了控制器测试:
RSpec.describe UsersController, type: :controller do
...
it do
patch :update, id: user.id.to_s, user: params
expect(response).to be_redirect
expect(assigns[:user]).to eq user
end
我想用调试器调试控制器,所以我在控制器的开头添加了byebug
def update
byebug
@user.attributes = user_params
unless @user.changed?
flash[:notice] = "No changes found in user"
render :edit
else
if @user.save
flash[:notice] = "User has been updated"
redirect_to edit_user_path(@user)
else
flash.now[:alert] = "User has not been updated"
render :edit
end
end
end
byebug不会在那里暂停执行,我什至尝试用byebug
替换raise "update"
只是为了看看事情是否过去了。他们不是。
答案 0 :(得分:0)
实际上我尚未登录,因此未执行控制器操作,因为rspec身份验证失败。
快速查看devise manual
请注意,确保您加入rails_helper.rb
:
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, :type => :controller
end
,并有一个spec/support
文件,指定:
def login_admin
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryBot.create(:admin) # Using factory bot as an example
end
end
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryBot.create(:user)
user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
sign_in user
end
end
end
然后只需在控制器rspec测试中调用正确的登录方法即可。