Я новичок в Rails и слежу за книгой «Гибкая веб-разработка с Rails». В настоящее время я проверяю свою корзину и позиции.
В моих светильниках у меня 2 позиции. Я удаляю один в тесте, так что еще один остается в корзине.
Однако я думаю, что благодаря моему сессионному механизму контроллер теперь использует новую пустую корзину вместо корзины с приборами. Это привело к провалу моего теста.
В моем * application_controller.rb *
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
en
В моем * line_items_controller.rb * у меня есть:
Def Destroy
@line_item = LineItem.find (params [: id])
@ Line_item.destroy
respond_to do |format|
if current_cart.line_items.empty?
format.html { redirect_to(store_url, :notice => 'Your cart is empty') }
else
format.html { redirect_to(current_cart, :notice => 'Item removed' )}
end
format.xml { head :ok }
end
конец
А в моем * функционале \ line_items_controller_test.rb * у меня есть:
test "should destroy line_item" do
assert_difference('LineItem.count', -1) do
delete :destroy, :id => @line_item.to_param
end
assert(!@cart.line_items.empty?,'Cart should not be empty')
if @cart.line_items.empty?
assert_redirected_to store_url #this was the redirection result
else
assert_redirected_to cart_path(@cart.id) #but should be this redirection
end
end
Код работает в реальной среде, просто тест не пройден.
Как я могу изменить свой тест и код, чтобы fixture-cart и мой механизм сессии могли работать вместе и пройти тест?