**********编辑**********
我对此进行了进一步调查,发现问题出在 FactoryGirl 传递附件的方式上。基本上,控制器接收的是一个字符串,而不是上传的文件对象。我创建了一个新问题(使用 FactoryGirl 时 Paperclip::Attachment 作为字符串传递),其中包括我为可能有类似问题的任何人提供的解决方法。
在 rails 3.2.6 上测试回形针 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
由于这是一个基本测试,我确信我刚刚遇到了一些可怕的错误。任何帮助将不胜感激!谢谢