大家好我拥有以下控制器代码:
class FeedbacksController < ApplicationController
def create
@feedback = Feedback.create(feedback_params)
if @feedback.errors.any?
flash[:error] = @feedback.errors
render 'new'
else
redirect_to :back
end
end
测试规范
require "rails_helper"
RSpec.describe FeedbacksController do
describe "POST create" do
context 'when param[:name] is present' do
it 'should redirect to homepage' do
@feedback = Feedback.create(:name => "Hah")
@feedback.save
is_expected.to redirect_to new_feedback_path
debugger
end
end
end
end
然而,当我运行localhost时,输出正是我想要的,但是在单元测试中,它没有通过但是返回我 &#34; m的预期响应是&lt; 200&gt; &#34;
我可以知道为什么会这样,我应该如何通过我的测试用例?
答案 0 :(得分:1)
您错过了对控制器的实际发布请求。
所以这个:
@feedback = Feedback.create(:name => "Hah")
@feedback.save
不应该在那里。无论如何第二行是多余的,创建已保存对象。
您正在寻找这样的代码:
it 'should redirect to homepage' do
post feedback_path, params: { name: 'hah' }
expect(response).to redirect_to(new_feedback_path)
end