That`s my carts_controller.rb. By default controller action add renders add.js.erb in /app/views/carts/
class CartsController < ApplicationController
def add
find_cart_and_product
@cart.products << @product
CartMailer.product_added(@product).deliver_now
end
end
and my test
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
has failed with common error for all test items:
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: []
I consider problem with expected requesting format, so how to force tests render with request.formats: ["application/javascript"] instead ["text/html"]?
This works for me (Rspec 3):
before :each do
request.headers["accept"] = 'application/javascript'
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.