在我的一项测试中,我必须验证某个优惠会在可用限额(例如10个座位,例如该优惠)全部售出或已过期时首先返回404响应。
我无法让PUT请求在Request Spec中工作,这是我的代码
RSpec.describe "Offers", type: :request do
describe "gives not found response" do
it "when available limit exhausts before date till available" do
offer = Offer.new
Timecop.freeze(Date.today - 12.days) do
offer = FactoryGirl.create(:offer)
end
payload = FactoryGirl.attributes_for(:offer, :available_limit => 0, :discount_id => offer.discount.id, :coupon_id => offer.coupon.id)
sign_in
put '/offers/'<<offer.id, params: { id: offer.id, offer: payload }, as: :json
get "/vouchers/"<<offer.coupon.voucher_code
expect(response.status).to eq 404
end
end
end
说我尝试了很多黑客,包括
put '/offers/'<<offer.id.to_s<<".json", params: { offer: payload }
甚至
put '/offers/'<<offer.id.to_s<<".json", payload
我还注意到,在其中一个组合中,请求确实通过但它以HTML和JSON格式响应,这导致规范中的错误,因为我没有在capybara下运行它们(我不想或者)
答案 0 :(得分:2)
不要使用<<
来构建这些路径。使用带有id的<<
将插入等同于id的整数值的字符代码,例如
'XYZ' << 123 << 'ABC'
=> "XYZ{ABC"
只需使用普通的字符串插值,例如
put "/offers/#{offer.id}", params: { id: offer.id, offer: payload }, format: :json
get "/vouchers/#{offer.coupon.voucher_code}"
还注意到@ house9也注意到format: :json
部分。
答案 1 :(得分:1)
我认为你想使用format: :json
而不是as: :json
尝试:
params = { id: offer.id, offer: payload }
put :offers, params: params, format: :json