【问题标题】:File Upload Using Paperclip With Rspec Testing (ruby on rails) [duplicate]使用带有 Rspec 测试的回形针上传文件(ruby on rails)[重复]
【发布时间】:2012-10-20 09:48:04
【问题描述】:
可能重复:
test a file upload using rspec - rails
我不知道如何为文件上传编写测试用例。你能帮帮我吗
我有包含用户照片、姓名、电子邮件的表格。我为姓名和电子邮件编写测试用例
喜欢
fill_in "user_name", :with => "xyz"
fill_in "user_email", :with => "test@test.com"
但是如何为选择照片编写测试用例?
【问题讨论】:
标签:
ruby-on-rails
ruby-on-rails-3
rspec
paperclip
rspec-rails
【解决方案1】:
如果你用过回形针,那么我可以帮助你..
这样写你的测试用例
def do_upload_file
# write the request for the action as defined in your routes file
# get :upload_file, :id => :user_id (id of the user)
end
before do
# set the session variables and other parameters here..
end
it "should upload the file correctly" do
@user.should_receive(:update_attributes).and_return(true)
do_upload_asset
response.should redirect_to(whatever_path)
end
对我有用,希望也对你有用....
【解决方案2】:
你可以这样做:
obj.asset = fixture_file_upload('/dummy_file.png', 'image/png')
您可以在规范助手中将夹具路径定义为:
config.fixture_path = "#{Rails.root}/spec/fixtures"
Official documentation