[英]How to make request specs for rails controller - request not hitting controller method
I am trying to write specs for a controller but my tests don't seem to be hitting the controller action.我正在尝试为 controller 编写规范,但我的测试似乎没有达到 controller 动作。
My controller:我的 controller:
class CoursesController < ApplicationController
def elearning_course_removal
Rails.logger.warn("warning!")
study_group = StudyGroup.where(id: params[:study_group_id]).first
course = Course.where(id: params[:id]).first
if study_group && course
Workflow.new.remove_elearning_course(study_group, course, current_user.id)
flash[:notice] = t('study_groups.courses.remove_elearning_course.enqueue', course_name: course.name)
else
Rails.logger.error(elearning_removal_error(study_group, course))
flash[:notice] = t('study_groups.courses.remove_elearning_course.enqueue_failure')
end
redirect_to :back
end
end
My test:我的测试:
require 'spec_helper'
describe CoursesController do
describe '#elearning_course_removal' do
let(:course) { FactoryBot.create(:course_with_files) }
let(:study_group) { FactoryBot.create(:study_group) }
let(:user) { FactoryBot.create(:user) }
let(:params) { {study_group_id: study_group.id, id: course.id, current_user: user} }
it 'logs' do
expect(Rails.logger).to receive(:warn)
# put :elearning_course_removal, params
controller.elearning_course_removal
end
end
Calling the route like this put:elearning_course_removal, params
in the spec just fails saying that logger wasn't called.像这样调用路径
put:elearning_course_removal, params
规范中的参数只是失败,说没有调用记录器。 Using the second style, controller.elearning_course_removal
will hit my controller method, but it's not standard, and I don't know how I would pass params in this case.使用第二种样式,
controller.elearning_course_removal
将达到我的 controller 方法,但这不是标准的,我不知道在这种情况下我将如何传递参数。 Its not a routing issue, as it is identifying the controller
variable correctly.它不是路由问题,因为它正确识别了
controller
变量。
What do I need to be able to write request specs for this controller?我需要什么才能为这个 controller 编写请求规范?
I think that you are not sending properly the params我认为您没有正确发送参数
RSpec.describe CoursesController do
describe "GET elearning_course_removal" do
let(:course) { FactoryBot.create(:course_with_files) }
let(:study_group) { FactoryBot.create(:study_group) }
let(:user) { FactoryBot.create(:user) }
let(:params) { {study_group_id: study_group.id, id: course.id, current_user: user} }
it "logs" do
expect(Rails.logger).to receive(:warn)
put :elearning_course_removal, params: params
end
end
you can pass params like this你可以像这样传递参数
params: {study_group_id: study_group.id, id: course.id, current_user: user}
params: params
or directly或直接
put :elearning_course_removal, params: {study_group_id: study_group.id, id: course.id, current_user: user}
what you are doing is this你在做什么是这个
put :elearning_course_removal, {study_group_id: study_group.id, id: course.id, current_user: user}
params tag is missing which could be the issue缺少参数标签,这可能是问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.