我正在开发一个小型电子商务,在为购物车控制器运行测试时遇到问题。
此处的购物车控制器
class CartsController < ApplicationController
include CurrentCart #modul current_cart in controllers/concerns
before_action :set_cart, only: [:show, :edit, :update, :destroy]
before_action :set_current_cart, only: [:index]
# GET /carts
# GET /carts.json
def index
respond_to do |format|
format.html { redirect_to @cart, notice: 'Line item was successfully created.' }
format.json { render :show, status: :created, location: @line_item }
end
end
# GET /carts/1
# GET /carts/1.json
def show
end
# GET /carts/new
def new
@cart = Cart.new
end
# GET /carts/1/edit
def edit
end
# POST /carts
# POST /carts.json
def create
@cart = Cart.new(cart_params)
respond_to do |format|
if @cart.save
format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: @cart }
else
format.html { render :new }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
respond_to do |format|
if @cart.update(cart_params)
format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: @cart }
else
format.html { render :edit }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to catalog_index_url, notice: 'Cart was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
@cart = Cart.includes(line_items: :product).find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params.fetch(:cart, {})
end
end
这里是我的测试
require 'test_helper'
class CartsControllerTest < ActionDispatch::IntegrationTest
setup do
@line_item = line_items(:one)
@product = products(:one)
@cart = carts(:one)
end
test "should get index" do
get carts_url
assert_response :success
end
test "should get new" do
get new_cart_url
assert_response :success
end
test "should create cart" do
assert_difference('Cart.count') do
post carts_url, params: { cart: {} }
end
assert_redirected_to cart_url(Cart.last)
end
test "should show cart" do
get cart_url(@cart)
assert_response :success
end
test "should get edit" do
get edit_cart_url(@cart)
assert_response :success
end
test "should update cart" do
patch cart_url(@cart), params: { cart: {} }
assert_redirected_to cart_url(@cart)
end
test "should destroy cart" do
post line_items_url, params: { product_id: products(:one).id }
@cart = Cart.find(session[:cart_id])
assert_difference('Cart.count', -1) do
delete cart_url(@cart)
end
assert_redirected_to catalog_index_url
end
end
结果在这里:
故障:CartsControllerTest#test_should_destroy_cart预期 响应为<3XX:redirect>,但响应为<500:Internal Server 错误>
故障:CartsControllerTest#test_should_get_edit预期响应 表示为<2XX:成功>,但表示为<500:内部服务器错误>
故障:CartsControllerTest#test_should_update_cart预期 响应为<3XX:redirect>,但响应为<500:Internal Server 错误>
这是cartsController的模块化当前购物车:
module CurrentCart
private
def set_current_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end
end
我认为问题出在这里: 如果我从购物车控制器:.includes(line_items::product)的set_cart方法中删除它,则测试将完美运行而不会失败,但是我不明白为什么。 (我放入了include,因为否则从浏览器访问购物车时会出现此错误:
使用急于加载检测到的LineItem => [:product]添加到您的 finder::includes => [:product]调用堆栈
更新:
我在文件“ test.log”中发现了一些有趣的东西:
Bullet::Notification::UnoptimizedQueryError - user: claudio
DELETE /carts/980190963
AVOID eager loading detected
LineItem => [:product]
Remove from your finder: :includes => [:product]
Call stack
##here there's a path##.rb:45:in `block (2 levels) in <class:CartsControllerTest>'
##here there's a path##:in `block in <class:CartsControllerTest>'