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

Testy Ruby on Rails - Destroy

Object Storage Arubacloud
0 głosów
371 wizyt
pytanie zadane 6 września 2016 w Ruby przez Szalbik Początkujący (430 p.)
edycja 7 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

 

komentarz 6 września 2016 przez dikons Użytkownik (540 p.)
Byłoby dużo czytelniej gdybyś odpowiednio wkleił kod. ;)
komentarz 6 września 2016 przez dikons Użytkownik (540 p.)
Pokaż proszę cały kod kontrolera ( jak inicjalizowana jest zmienna @cart) i metodę setup z testu. Ewentualnie podrzuć link do repozytorium git Twojego projektu. Chętnie pomogę, ale na razie mam za mało informacji, można się jedynie domyślać co jest nie tak
komentarz 7 września 2016 przez Szalbik Początkujący (430 p.)
Kontroller

 

 

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

  # 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

  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

 

komentarz 7 września 2016 przez Szalbik Początkujący (430 p.)

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

 

komentarz 7 września 2016 przez Szalbik Początkujący (430 p.)

Chodzi tutaj o "should destroy line_item". Generuje mi taki błąd:

Error:
LineItemsControllerTest#test_should_destroy_line_item:
ActiveRecord::RecordNotFound: Couldn't find LineItem with 'id'=1 [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>'

 

komentarz 7 września 2016 przez Szalbik Początkujący (430 p.)

A tutaj mam fixtury dla lineitems: 

 

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

 

1 odpowiedź

0 głosów
odpowiedź 6 września 2016 przez event15 Szeryf (93,790 p.)
To są, przepraszam za wścibskość, testy jednostkowe? Bo nie ma miejsca w jednostkowych testach na testowanie bazy danych. Znasz pan pojęcie stuba, mocka, fake'a?
komentarz 7 września 2016 przez Szalbik Początkujący (430 p.)
Nie znam. Jedem początkujący i zaczynam od tej książki ...

Podobne pytania

0 głosów
0 odpowiedzi 418 wizyt
pytanie zadane 16 października 2020 w Ruby przez belkocik Początkujący (330 p.)
0 głosów
0 odpowiedzi 282 wizyt
0 głosów
2 odpowiedzi 1,423 wizyt

92,570 zapytań

141,422 odpowiedzi

319,643 komentarzy

61,958 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

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy 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!

...