RSpec 测试因文件字段而失败

问题描述 投票:0回答:0

当我测试我的图库控制器时,出现以下错误:

Failure/Error: expect(response).to redirect_to show_profile_show_profile_path
     
       Expected response to be a <3XX: redirect>, but was a <200: OK>
       Response body: 
     # ./spec/controllers/gallery_controller_spec.rb:17:in `block (3 levels) in <top (required)>'

这是画廊表的架构:

create_table "galleries", id: :serial, force: :cascade do |t|
    t.string "gallery_title"
    t.text "gallery_description"
    t.string "gallery_picture", array: true
    t.float "gallery_totalRate"
    t.integer "gallery_totalRator"
    t.integer "GeneralInfo_id"
    t.string "test_picture", array: true
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "gallery"
    t.text "comments", default: [], array: true
  end

这是我的画廊模型:


class Gallery < ApplicationRecord
    belongs_to :general_info, optional: true
    has_many :reviews
    has_many :gallery_taggings, dependent: :destroy
    has_many :tagged_users, through: :gallery_taggings, source: :general_info

    validates_presence_of :gallery_title
    validates_presence_of :gallery_description
    validates_presence_of :gallery_picture
    validates_presence_of :GeneralInfo_id
    mount_uploaders :gallery_picture, GalleryUploader

    mount_uploaders :test_picture, GalleryUploader
    has_many :comments

end
    

这是我正在尝试测试的方法:

def create
    @general_info = GeneralInfo.find_by(userKey: session[:current_user_key])
    params[:gallery][:GeneralInfo_id] = @general_info.id
    @gallery = Gallery.new(gallery_params)
    if @gallery.gallery_picture.length == 0
      flash.now[:error] = "You should add at least 1 image!"
      render 'new'

    elsif @gallery.gallery_picture.length > 5 
      flash.now[:error] = "You can't add more than 5 images!"
      render 'new'

    
    elsif @gallery.save!
      flash[:notice] = "Project Created"

      redirect_to '/show_profile'
    else
      render 'new'
    end
  end

这是我的测试:

session[:current_user_key] = SecureRandom.hex(10)
@general_info = GeneralInfo.create(:first_name => "R", :last_name => "Spec", :highlights => "test highlights", :company => "test company", :industry => "test industry", :emailaddr => "[email protected]", :phone =>892, :month_ofbirth => 01,:day_ofbirth => 31, :year_ofbirth => 1985, :gender => "Female", :country => "United States", :state => "TX", :city => "Austin", userKey: session[:current_user_key])
post :create, params: { gallery: {GeneralInfo_id: 1, gallery_title: "test", gallery_description: "test", gallery_picture: ["/path/to/file/background-creative.jpg"]}}
expect(response).to redirect_to show_profile_show_profile_path

由于某种原因,create 方法认为 params 哈希中 gallery_picture 为空,即使那里有一个文件。任何帮助将不胜感激。

ruby-on-rails activerecord rspec rails-activerecord
© www.soinside.com 2019 - 2024. All rights reserved.