0
有問題測試(工作)上傳在我的Rails應用程序(使用2.3.8):測試文件上傳的Rails:不能轉換的ActionController :: TestUploadedFile轉換成String
class ProfilesControllerTest < ActionController::TestCase
test "creates a new profile" do
fixture_image = fixture_file_upload("#{RAILS_ROOT}/test/fixtures/files/avatar.jpg", 'image/jpeg')
post :create, :profile=>{:username=>'johndoe',
:password=>'mypass',
:avatar => fixture_image
}, :html => { :multipart => true }
assert_response :success
assert_not_nil Profile.find_by_username("johndoe")
assert_not_nil Profile.find_by_username("johndoe").avatar
end
end
控制器剛剛分配散裝PARAMS
@profile = Profile.new(params[:profile])
@profile.save
模型使用Joint處理上傳:
class Profile
include MongoMapper::Document
plugin Joint
attachment :avatar
end
運行測試時出現此錯誤:
1) Error:
test_creates_a_new_profile(Api::ProfilesControllerTest):
TypeError: can't convert ActionController::TestUploadedFile into String
(eval):15:in `size'
(eval):15:in `avatar='
/Users/oliver/.rvm/gems/ruby-1.8.7-p302/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/keys.rb:183:in `send'
什麼給出了?顯然avatar = setter將處理真正上傳的文件,但不會處理TestUploadedFile的。
謝謝約翰。看起來比我更漂亮的比我的http://ap.rubyonrails.org/classes/ActionController/TestUploadedFile.html :)我忽略了它是File.size是在測試文件上進行釣魚。 – oliverbarnes 2010-11-04 07:02:56