![](/img/trans.png)
[英]ParksController#create is missing a template for this request format and variant. request.formats: [“text/html”] request.variant: []
[英]Substitution of request.formats: [“text/html”] with [“application/javascript”]
那是我的carts_controller.rb。 默認情況下,控制器操作添加在/ app / views / carts /中呈現add.js.erb
class CartsController < ApplicationController
def add
find_cart_and_product
@cart.products << @product
CartMailer.product_added(@product).deliver_now
end
end
和我的考驗
describe 'POST #add' do
let(:cart_full_of){ create(:cart_with_products) }
let(:product){ create(:product) }
before do
post :add, session: { cart_id: cart_full_of.id }, params: { product_id: product.id}
end
it { expect(response.status).to eq(200) }
it { expect(response.headers["Content-Type"]).to eql("application/javascript"; charset=utf-8")}
it { is_expected.to render_template :add }
it 'should add current product into cart' do
expect(cart_full_of.products).to eq([product])
end
end
所有測試項目的常見錯誤都失敗了:
Failure/Error: post :add, session: { cart_id: cart_full_of.id }, params: { product_id: product.id}
ActionController::UnknownFormat:
CartsController#add is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
我考慮了預期請求格式的問題,所以如何強制使用request.formats進行測試渲染:[“application / javascript”]而不是[“text / html”]?
這對我有用(Rspec 3):
before :each do
request.headers["accept"] = 'application/javascript'
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.