2012-05-25 28 views
0

我破壞了LINE_ITEM模型單元測試與錯誤不合格品「無法找到ID = 1的產品」。看來Rails不能銷燬我的line_item,因爲它從數據庫中獲取它時會引發異常。這是我的LineItem模型:敏捷Web開發使用Rails:無法找到ID = 1

class LineItem < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :cart 

    def total_price 
    product.price * quantity 
    end 
end 

這裏是測試套件:

require 'test_helper' 

class LineItemsControllerTest < ActionController::TestCase 
    setup do 
    @line_item = line_items(:one) 
    Rails.logger.debug(@line_item.to_yaml) 
    Rails.logger.debug(Product.all.to_yaml) 
    end 

    test "should get index" do 
    get :index 
    assert_response :success 
    assert_not_nil assigns(:line_items) 
    end 

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

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

    assert_redirected_to cart_path(assigns(:line_item).cart) 
    end 

    test "should show line_item" do 
    get :show, id: @line_item 
    assert_response :success 
    end 

    test "should get edit" do 
    get :edit, id: @line_item 
    assert_response :success 
    end 

    test "should update line_item" do 
    put :update, id: @line_item, line_item: @line_item.attributes 
    assert_redirected_to line_item_path(assigns(:line_item)) 
    end 

    test "should destroy line_item" do 
    Rails.logger.debug "Breaking!" 
    assert_difference('LineItem.count', -1) do 
     delete :destroy, id: @line_item 
    end 

    assert_redirected_to cart_path(path) 
    end 
end 

這裏是我記錄的部分:

Breaking! 
    [1m[35m (0.1ms)[0m SELECT COUNT(*) FROM "line_items" 
Processing by LineItemsController#destroy as HTML 
    Parameters: {"id"=>"980190962"} 
    [1m[36mLineItem Load (0.1ms)[0m [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1[0m [["id", "980190962"]] 
    [1m[35mProduct Load (0.1ms)[0m SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 1]] 
Completed 500 Internal Server Error in 3ms 
    [1m[36m (0.1ms)[0m [1mrollback transaction[0m 
    [1m[35m (0.1ms)[0m begin transaction 
    [1m[36mLineItem Load (0.1ms)[0m [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1[0m [["id", 980190962]] 
--- !ruby/object:LineItem 
attributes: 
    id: 980190962 
    product_id: 1 
    cart_id: 1 
    created_at: 2012-05-25 20:37:17.000000000 Z 
    updated_at: 2012-05-25 20:37:17.000000000 Z 
    quantity: 1 
    product_price: 

    [1m[35mProduct Load (0.2ms)[0m SELECT "products".* FROM "products" 
--- 
- !ruby/object:Product 
    attributes: 
    id: 207281424 
    title: Programming Ruby 1.9 
    description: Ruby is the fastest growing and most exciting dynamic language out 
     there. If you need to get working programs delivered fast, you should add Ruby 
     to your toolbox. 
    image_url: ruby.png 
    price: 49.5 
    created_at: 2012-05-25 20:37:17.000000000 Z 
    updated_at: 2012-05-25 20:37:17.000000000 Z 

謝謝!

編輯:這裏是產品型號:

class Product < ActiveRecord::Base 
    has_many :line_items 

    before_destroy :ensure_not_referenced_by_any_line_item 

    # Validation 
    validates :title, :description, :image_url, presence: true 
    validates :title, length: { 
    minimum: 10, 
    message: "must be at least %{count} characters long" 
    } 
    validates :price, numericality: { greater_than_or_equal_to: 0.01 } 
    validates :title, uniqueness: true 
    validates :image_url, allow_blank: true, format: { 
    with: %r{\.(gif|jpg|png)$}i, 
    message: 'must be a URL for GIF, JPG or PNG image.' 
    } 

    private 

    def ensure_not_referenced_by_any_line_item 
    if line_items.empty? 
     return true 
    else 
     errors.add :base, 'Line items present' 
     return false 
    end 
    end 
end 
+0

什麼是您的產品型號看起來像更換呢?它有:依賴=>:銷燬集? –

+0

我已添加產品模型。謝謝。 – StockBreak

回答

1

這是該工廠一般都優於在Rails社區燈具的原因之一。燈具不會自動加載它們的關聯,並因此而變得脆弱。您的夾具具有1的product_id,但該產品不存在。

我不知道究竟如何解決您的特定問題,但我會建議您:

  1. 在您的測試ID = 1創建產品。
  2. 創建一個ID = 1的產品,夾具和加載它在你的設置
  3. 切換到工廠(最好是這樣)

編輯

如上所述here,您還可以使用標籤協會參考。所以,如果你有所謂的「TV」產品夾具,你可以刪除你的LINE_ITEM燈具的PRODUCT_ID字段並用product: tv

+0

謝謝!我認爲現在我會保持簡單,並添加一個特定的夾具。我將尋找工廠,當我完成這本書:)我很奇怪,爲什麼沒有人在網絡上的其他人有這個問題... – StockBreak

+0

更新我有一個更好的方法的答案。 –

+0

謝謝你,我會用標籤。 – StockBreak

相關問題