• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Integration test destroy

VPS Starter Arubacloud
0 głosów
221 wizyt
pytanie zadane 8 września 2016 w Ruby przez Szalbik Początkujący (430 p.)
edycja 8 września 2016 przez Szalbik

Witam, bardzo proszę o pomoc. Uczę się Rails z pomocą książki Agile Web Development with Rails 5 i napotkałem na problem. Za zadanie mam sprawić aby produkt z koszyka się usunął. Myślałem, że będzie to proste jednak się przeliczyłem.

błąd jaki mi wyskakuje to:
 

Error:
LineItemsControllerTest#test_should_destroy_line_item:
ActiveRecord::RecordNotFound: Couldn't find LineItem with 'id'=980190962 [WHERE "line_items"."cart_id" = ?]
    app/controllers/line_items_controller.rb:62:in `destroy'
    test/controllers/line_items_controller_test.rb:58:in `block (2 levels) in <class:LineItemsControllerTest>'
    test/controllers/line_items_controller_test.rb:57:in `block in <class:LineItemsControllerTest



 

Akcja w kontrolerze:

class LineItemsController < ApplicationController
  include CurrentCart
  before_action :set_cart, only: [:create, :destroy, :decrement]
  before_action :set_line_item, only: [:show, :edit, :update, :destroy, :decrement]

  # GET /line_items
  # GET /line_items.json
  def index
    @line_items = LineItem.all
  end

  # GET /line_items/1
  # GET /line_items/1.json
  def show
  end

  # GET /line_items/new
  def new
    @line_item = LineItem.new
  end

  # GET /line_items/1/edit
  def edit
  end

  # POST /line_items
  # POST /line_items.json
  def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product)

    respond_to do |format|
      if @line_item.save
        session[:counter] = 0
        format.html { redirect_to store_index_url }
        format.js { @current_item = @line_item }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /line_items/1
  # PATCH/PUT /line_items/1.json
  def update
    respond_to do |format|
      if @line_item.update(line_item_params)
        format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
        format.json { render :show, status: :ok, location: @line_item }
      else
        format.html { render :edit }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /line_items/1
  # DELETE /line_items/1.json
  def destroy
    @line_item = @cart.line_items.find(params[:id])
    @line_item.destroy 
    
    respond_to do |format|
      format.html { redirect_to store_index_url }
      format.json { head :no_content }
    end
  end
  
  # POST /line_items/1
  # POST /line_items/1.json
  
  def decrement
    @line_item.decrement
    
    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_index_url }                          
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_line_item
      @line_item = LineItem.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def line_item_params
      params.require(:line_item).permit(:product_id)
    end
end

Test:

require 'test_helper'

class LineItemsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @line_item = line_items(:one)
  end

  test "should get index" do
    get line_items_url
    assert_response :success
  end

  test "should get new" do
    get new_line_item_url
    assert_response :success
  end

  test "should create line_item" do
    assert_difference('LineItem.count') do
      post line_items_url, params: { product_id: products(:ruby).id }
    end

    follow_redirect!
    
    assert_select 'h2', "Your Cart"
    assert_select 'td', "Programming Ruby 1.9"
  end
  
  test "should create line_item via ajax" do 
    assert_difference('LineItem.count') do 
      post line_items_url, params: { product_id: products(:ruby).id },
        xhr: true
    end
    
    assert_response :success
    assert_select_jquery :html, '#cart' do
      assert_select 'tr#current_item td', /Programming Ruby 1.9/
    end
  end

  test "should show line_item" do
    get line_item_url(@line_item)
    assert_response :success
  end

  test "should get edit" do
    get edit_line_item_url(@line_item)
    assert_response :success
  end

  test "should update line_item" do
    patch line_item_url(@line_item), params: { line_item: { product_id: @line_item.product_id } }
    assert_redirected_to line_item_url(@line_item)
  end

  test "should destroy line_item" do
    assert_difference('LineItem.count', -1) do
      delete line_item_url(line_items(:o1_coffee))
    end

    assert_redirected_to store_index_url
  end
end

fixtury line_items.yml:

one:
  product: two
  cart: one
  price: 12

two:
  product: two
  cart: two
  price: 15
  
o1_coffee:
  id: 1
  product_id: 1
  cart_id: 1
  quantity: 2
  price: 36.00

o1_ruby:
  product_id: 2
  cart_id: 1
  quantity: 2
  price: 49.95

o1_rails:
  product_id: 3
  cart_id: 1
  quantity: 2
  price: 34.95

koszyk ustawiam w controllers/concerns/current_cart.rb:

module CurrentCart
  
  private
    
    def set_cart
      @cart = Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      @cart = Cart.create
      session[:cart_id] = @cart.id
    end
  
end

 

Wg. mnie problem jest w przekazywaniu z test_controller destroy parametru sesji dla cart_id. Co bym nie zrobił nie potrafię tego przekazać.

1 odpowiedź

0 głosów
odpowiedź 8 września 2016 przez Bantu Nałogowiec (34,170 p.)
Obstawiam, że w Twoim modelu, LineItem, pole product i cart są relacjami, więc dlaczego w fixtures masz je jako stringi podane?
komentarz 9 września 2016 przez Szalbik Początkujący (430 p.)
Poprawiłem i nadal błąd

Podobne pytania

0 głosów
1 odpowiedź 362 wizyt
pytanie zadane 6 września 2016 w Ruby przez Szalbik Początkujący (430 p.)
0 głosów
1 odpowiedź 127 wizyt
pytanie zadane 24 lipca 2021 w Ruby przez ::{}:: Gaduła (3,890 p.)
0 głosów
0 odpowiedzi 178 wizyt
pytanie zadane 9 listopada 2019 w Ruby przez reaktywny Nałogowiec (40,650 p.)

92,453 zapytań

141,262 odpowiedzi

319,088 komentarzy

61,854 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Akademia Sekuraka 2024 zapewnia dostęp do minimum 15 szkoleń online z bezpieczeństwa IT oraz dostęp także do materiałów z edycji Sekurak Academy z roku 2023!

Przy zakupie możecie skorzystać z kodu: pasja-akademia - użyjcie go w koszyku, a uzyskacie rabat -30% na bilety w wersji "Standard"! Więcej informacji na temat akademii 2024 znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...