首先介绍下我的模型设计
class User < ActiveRecord::Base
include Paperclip::Glue

attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" },
                                        :default_url => "/p_w_picpaths/:style/missing.png"
end
页面这些就不介绍了,现在说下FactoryGirl怎么构建测试用例
FactoryGilr.define :photo,class=>userdo
 avatar {fixture_file_upload(Rails.root.to_s +  '/public/system/users/avatars/test.jpg','p_w_picpath/jpg')}
end
我们使用了fixture_file_upload这个测试夹具,模拟文件上传
然后我们在User_model_Rspec测试
describe "关于照片的测试" do
     before (:each)do
        @photo = Factory.create(:photo)
     end
    it"是否存入指定文件夹位置"do
     File.should be_exists(@photo.avatar.path(:medium)) 
    end
end
我们可以使用avatar.path(:medium)来获取文件存放地址。