RSpec 测试未通过:预期响应为 <3XX: redirect>,但为 <200: OK>
RSpec test not passing: Expected response to be a <3XX: redirect>, but was a <200: OK>
我在 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>
我已经尝试了一段时间的故障排除,但不确定我哪里搞砸了?有任何想法吗?谢谢!
您实际上并不是在调用您的创建操作。你有...
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 制作广告。您需要实际调用 post 操作。
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
我在 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>
我已经尝试了一段时间的故障排除,但不确定我哪里搞砸了?有任何想法吗?谢谢!
您实际上并不是在调用您的创建操作。你有...
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 制作广告。您需要实际调用 post 操作。
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