2016-03-27 20 views
1

Geçiş yapmak için bir entegrasyon testi yapmayla ilgili sorun yaşıyorum. Bir kullanıcı çok sayıda resme sahip bir ürün yaratır. Ürünün new.html.erb dosya alanı <%= file_field_tag "images[]", type: :file, multiple: true %> içerir. Kod düzgün çalışıyor, sadece çalışan bir entegrasyon testi oluşturmada problem yaşıyorum.Rails, bir dizi dosya yüklemek için entegrasyon testi yazmada sorun yaşıyor

products_creation_test.rb:

require 'test_helper' 

class ProductsCreationTest < ActionDispatch::IntegrationTest 
    . 
    . 
    . 
    test "should create new product with valid info" do 
    log_in_as(@user) 
    pic1 = fixture_file_upload('test/fixtures/cat1.jpg', 'image/jpg') 
    pic2 = fixture_file_upload('test/fixtures/profile.png', 'image/png') 
    assert_difference 'Product.count', 1 do 
     post user_products_path(@user), product:  { title:  'test', 
                description: 'test', 
                price:  '5.99', 
                images: [pic1, pic2] } 
    end 
    assert_redirected_to @user 
    @user.reload 
    newprod = @user.products.last 
    pics = newprod.pictures.all 
    assert_equal 2, pics.count 
    end 
end 

Bu yeni ürün ile ilişkili resmim yok belirterek, son iddiayı başarısız, 2. pics değişken teftiş olmalı, ben aşağıdaki hatayı alıyorum: RuntimeError: #<ActiveRecord::AssociationRelation []

Neyi eksik? aşağıdaki gibi

benim modeli yapısı şöyledir: User Has_many Products, Products Has_many Pictures

products_controller.rb:

class ProductsController < ApplicationController 
    before_action :valid_user, only: [:new, :create, :edit, :update] 
    . 
    . 
    . 
    def create 
    @product = current_user.products.build(product_params) 
    if @product.save 
     # to handle multiple images upload on create 
     if params[:images] 
     params[:images].each { |image| 
      @product.pictures.create(image: image) 
     } 
     end 
     flash[:success] = "Product Created!" 
     redirect_to current_user 
    else 
     flash[:alert] = "Something went wrong." 
     render :new 
    end 
    end 
. 
. 
. 
end 

pictures_controller.rb:

class PicturesController < ApplicationController 

    def create 
    @picture = Picture.new(picture_params) 
    @picture.save 
    end 

    def destroy 
    end 

    private 
    def picture_params 
     params.require(:picture).permit(:product_id, :image, :_destroy)  
    end 
end 

picture.rb:

olarak çözülmüş
class Picture < ActiveRecord::Base 
    belongs_to :product 
    mount_uploader :image, PicturesUploader 

    validates_integrity_of :image 
    validates_processing_of :image 
    validates :image, file_size: { less_than: 10.megabytes } 
end 

cevap

1

aşağıdaki gibidir:

revize products_creation_test.rb:

require 'test_helper' 

class ProductsCreationTest < ActionDispatch::IntegrationTest 
    . 
    . 
    . 
    test "should create new product with valid info" do 
    log_in_as(@user) 
    pic1 = fixture_file_upload('test/fixtures/cat1.jpg', 'image/jpg') 
    pic2 = fixture_file_upload('test/fixtures/profile.png', 'image/png') 
    assert_difference 'Product.count', 1 do 
     post user_products_path(@user), product:  { title:  'test', 
                description: 'test', 
                price:  '5.99', 
                }, images: [pic1, pic2] 
    end 
    assert_redirected_to @user 
    newprod = assigns(:product) 
    assert_equal 2, newprod.pictures.count 
    end 
end 
İlgili konular