Tobias Grimm
2006-Dec-30 23:00 UTC
[rspec-users] Another RSpec on Rails issue: how to test send_file()
Hi! Today I needed to implement some controller code, that uses send_file() to send image data. How can this be tested / specified with RSpec? bye, Tobias
s.ross
2006-Dec-31 00:45 UTC
[rspec-users] Another RSpec on Rails issue: how to test send_file()
Well, here''s one I just dealt with. The spec might not be great, but here it is: # in spec_helper.rb (this code is taken pretty much intact from the Wiki article on testing uploads # get us an object that represents an uploaded file def uploaded_file(path, content_type="application/octet- stream", filename=nil) filename ||= File.basename(path) t = Tempfile.new(filename) FileUtils.copy_file(path, t.path) (class << t; self; end;).class_eval do alias local_path path define_method(:original_filename) { filename } define_method(:content_type) { content_type } end return t end # a JPEG helper def uploaded_jpeg(path, filename=nil) uploaded_file(path, ''image/jpeg'', filename) end # a GIF helper def uploaded_gif(path, filename=nil) uploaded_file(path, ''image/gif'', filename) end # a CSV helper def uploaded_csv(path, filename=nil) uploaded_file(path, ''text/csv'', filename) end # in your spec setup do get_logged_in @upload_file = uploaded_csv("#{File.expand_path(RAILS_ROOT)}/ spec/fixtures/upload_test.csv") @upload_record_count = File.open("#{File.expand_path (RAILS_ROOT)}/spec/fixtures/upload_test.csv").readlines("\r").size - 1 @original_size = Member.count end specify "the upload method should receive a good file" do @upload_file = uploaded_csv("#{File.expand_path(RAILS_ROOT)}/ spec/fixtures/upload_test.csv") post :upload, :member_file => @upload_file response.should_be_success Member.count.should == @original_size + @upload_record_count m = Member.find_by_first_and_last(''Blake'', ''Park'') m.should_not_be_nil m.created_on.year.should_be > 2000 end specify "the upload method should reject bogus data in a file" do @upload_file = uploaded_csv("#{File.expand_path(RAILS_ROOT)}/ spec/fixtures/upload_bogus_test.csv") post :upload, :member_file => @upload_file response.should_be_success response.should_have_tag :p, :content => /upload data error/ end Comments welcomed. Steve On Dec 30, 2006, at 3:00 PM, Tobias Grimm wrote:> Hi! > > Today I needed to implement some controller code, that uses > send_file() > to send image data. How can this be tested / specified with RSpec? > > bye, > > Tobias > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Tobias Grimm
2006-Dec-31 09:58 UTC
[rspec-users] Another RSpec on Rails issue: how to test send_file()
s.ross wrote:> Well, here''s one I just dealt with. The spec might not be great, but > here it is: >This is kinda different from what I want to do. My controller looks like this: ... def images image_file_name = get_image_file_name_by_id(params[:id]) send_file(image_file_name, :type => ''image/png'', :disposition => ''inline'') end .. I can test with response.should_be_success that this request works. setup do get(:images, :id => ''test-image'') end spec "should be successful" do response.should_be_success end spec "should return mime type image/png'' # dont know how to test this end spec "image data should be inline'' # dont know how to test this end spec "should return the content of the image file" # test-images simply contains "dummy-data" - but how do I check this? end But I don''t know how to check, if the content sent to the client with send_file is correct. During running the spec, the image file is a simple text file - But how do I get access to the content, the mime header and the content disposition field to check if these are correct? Tobias