![](/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.