我在ads_controller.spec.rb文件中设置了以下测试:
describe "ads#create action" do
it "redirects to ads#show" do
@ad = FactoryBot.create(:ad)
expect(response).to redirect_to ad_path(@ad)
end
end
与我的广告控制器中的此操作相对应:
def create
@ad = current_user.ads.create(ad_params)
redirect_to ad_path(@ad)
end
广告制作完成后,我希望它重定向到刚刚制作的广告的展示页面。虽然这在我的浏览器中有效,但我显然没有正确构建我的测试,因为我收到以下错误:
Failure/Error: expect(response).to redirect_to ad_path(@ad)
Expected response to be a <3XX: redirect>, but was a <200: OK>
我试图解决它一段时间而不确定我在哪里弄乱了?有任何想法吗?谢谢!
答案 0 :(得分:1)
您实际上并没有调用您的创建操作。你有......
describe "ads#create action" do
it "redirects to ads#show" do
@ad = FactoryBot.create(:ad)
expect(response).to redirect_to ad_path(@ad)
end
end
仅使用FactoryBot创建广告。你需要对后期行动进行实际调用。
RSpec.describe AdsController, type: :controller do
let(:valid_attributes) {
("Add a hash of attributes valid for your ad")
}
describe "POST #create" do
context "with valid params" do
it "redirects to the created ad" do
post :create, params: {ad: valid_attributes}
expect(response).to redirect_to(Ad.last)
end
end
end
end