我的应用程序将治疗作为一个任务列表进行管理。任务是通过从治疗显示视图调用的模式窗口创建和编辑的。当关闭任务表单时,I redirect_back
到治疗,root_path作为回退位置:
def update
@task.updated_by = current_login
respond_to do |format|
if @task.update_attributes(task_params)
format.html { redirect_back fallback_location: root_path, notice: t('.Success') }
format.json { head :no_content }
else
format.html { redirect_back fallback_location: root_path, notice: "#{t('.Failure')}: #{@task.errors.full_messages.join(',')}" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
字符串
使用Rspec测试总是会引发错误,因为它会重定向到root_path:
describe "update - PATCH /tasks/:id" do
context "with valid parameters" do
let(:new_attributes) do
{
sort_code: "RSPEC-Updated"
}
end
it "updates the requested task" do
patch task_url(task), params: {
task: new_attributes
}.merge(extra_fields)
expect(response).to redirect_to(treatment_url(task.parent))
end
end
型
如何配置测试以重定向到预期的位置并使其通过?
1条答案
按热度按时间7vhp5slm1#
redirect_back
方法使用request.referer
重定向到上一个位置:字符串
因此,您需要将
referer
设置为所需的URL(就像在您的情况下,它应该设置为treatment_url(task.parent)
)。你可以尝试在Rspec中设置
referer
,或者像上面提到的here on this answer:型