给出NoHandlerError的Paperclip功能测试

时间:2012-06-19 18:47:41

标签: ruby-on-rails ruby paperclip factory-bot

  

可能重复:
  Paperclip exception : Paperclip::AdapterRegistry::NoHandlerError

********** EDIT **********

我对此进行了进一步调查,发现问题归咎于FactoryGirl传递附件的方式。基本上控制器正在接收字符串而不是上传的文件对象。我创建了一个新问题(Paperclip::Attachment passed as string when using FactoryGirl),其中包含针对可能遇到类似问题的任何人的解决方法。

在rails 3.2.6上测试paperclip 3.1.2,我在测试创建带有文件附件的模型时遇到以下异常:

test_should_create_artist(ArtistsControllerTest):
Paperclip::AdapterRegistry::NoHandlerError: No handler found for "/system/artists/photos/000/000/001/original/rails.png?1340130452"

我的模特是:

class Artist < ActiveRecord::Base
  has_attached_file :photo, :styles => {:medium => "300x300>", :thumb => "100x100>" }, :default_url => "image_missing.png"
  attr_accessible :photo, :photo_file_name

  validates_attachment :photo, :presence => true, :content_type => { :content_type => "image/jpg" }, :size => { :in => 0..10000.kilobytes }
end

我的控制器是:

class ArtistsController < ApplicationController

  # POST /artists
  # POST /artists.json
  def create
    @artist = Artist.new(params[:artist])

    respond_to do |format|
      if @artist.save
        format.html { redirect_to @artist, notice: 'Artist was successfully created.' }
        format.json { render json: @artist, status: :created, location: @artist }
      else
        format.html { render action: "new" }
        format.json { render json: @artist.errors, status: :unprocessable_entity }
      end
    end
  end 
end

我的测试是:

require 'test_helper'

class ArtistsControllerTest < ActionController::TestCase
  setup do
    @artist = FactoryGirl.build(:artist)
  end

  test "should create artist" do
    assert_difference('Artist.count') do
      post :create, :artist => { biography: @artist.biography, name: @artist.name, website: @artist.website, photo: @artist.photo }, :html => { :multipart => true }
    end

    assert_redirected_to artist_path(assigns(:artist))
  end 
end

据我所知,工厂正在运作,但它是:

include ActionDispatch::TestProcess

FactoryGirl.define do
  factory :artist do
    id 1
    photo {fixture_file_upload(Rails.root.join('test','fixtures', 'files', 'rails.png'),'image/png')}
  end
end

由于这是一项基本测试,我确信我只是犯了一些可怕的错误。任何帮助将不胜感激!感谢

0 个答案:

没有答案