attachment_fu testing in rails 3
Asked Answered
A

3

11

I am trying to write specs for a working file upload functionality using attachment_fu. However, the sample code given by the author for testing requires me to require action_controller/test_process so that I have access to ActionController::UploadedStringIO class. I have used this before in rails 2.x but for rails 3, it fails to locate the test_process file.

How do I go ahead with testing the file upload functionality in rails 3?

Audrey answered 19/10, 2010 at 7:52 Comment(0)
C
23

I was able to use fixture_file_upload just fine with Rails3 and rspec.

I just added include ActionDispatch::TestProcess to the spec_helper.rb, and then passed something like the following as the filedata:

fixture_file_upload(Rails.root.join("spec/support/test.png"), 'image/png')

EDIT: Something I upgraded changed the behavior of this to always include the fixtures path, so I switched to this instead of fixture_file_upload:

Rack::Test::UploadedFile.new(Rails.root.join("spec/support/test.png"), 'image/png')
Chauchaucer answered 4/5, 2011 at 18:24 Comment(1)
+1 for the last comment on using Rack::Test::UploadedFile.new! with include ActionDispatch::TestProcess I was somehow getting strange behaviour with factory girl...Estranged
O
4

I'm working on testing file uploads in rails 3 right now, although I don't know how helpful my answer will be. Would it be possible for you to change the test? Personally, I'm using a Cucumber/Capybara approach. Capybara's DSL defines

attach_file('Image', '/path/to/image.jpg')

Personally this seems a better way to test than interacting with the StringIO object...feel free to retort.

Ovariotomy answered 26/10, 2010 at 17:30 Comment(0)
F
3

This was somewhat tricky to find for Rails 3

You can still use the fixture_file_upload function, but you have to add:

include ActionDispatch::TestProcess

To your test case.

This didn't work at all with Rspec. I just had to grab the file itself and post the file directly.

Finnigan answered 11/4, 2011 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.