[rspec-users] Testing file upload (Sinatra, RSpec, Rack-Test)

P. A. lists at ruby-forum.com
Sat Mar 13 13:40:32 EST 2010


Hi.

I have a simple application and I want to add a file upload
functionality. How can I write a spec?

Here's my try.

# directory structure
app:
 - controller.rb
 - controller.spec.rb
 - files: []
 - fixtures: [test_file.png]

# controller.spec.rb
require 'rack/test'
require 'controller'

module MyHelpers
 def app
  Sinatra::Application
 end
end

Spec::Runner.configure do |conf|
 conf.include Rack::Test::Methods
 conf.include MyHelpers
end

describe 'Application' do
 it 'should accept uploaded files and save them into the `files`
directory'
  post '/', 'file' =>
Rack::Test::UploadedFile.new('fixtures/test_file.png', 'image/png')
  Dir['files/*'].should include('files/test_file.png')
 end
end

# controller.rb
require 'sinatra'
require 'fileutils'

post '/' do
 tempfile = params['file'][:tempfile]
 FileUtils.copy_file(tempfile.path, 'files')
end

But when I ran...

$ spec -f specdoc controller.rb

...I get the following output.

Application
- should accept uploaded files and save them into the `files` directory
(FAILED - 1)

1)
'Application should accept uploaded files and save them into the `files`
directory' FAILED
expected [] to include "files/test_file.png"
<path_to_app>/controller.spec.rb:<line_number>:in `block (2 levels) in
<top (required)>'

Finished in 0.132799038 seconds

1 example, 1 failure

What I do wrong?

Thanks.

Debian GNU/Linux 5.0.4;
Ruby 1.9.2;
Sinatra 0.9.6;
RSpec 1.3.0;
Rack-Test 0.5.3.
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list