Ruby Forum RSpec > Attachment-fu + Story Runner

Posted by Daniel Tenner (Guest)
on 11.12.2007 14:44
(Received via mailing list)
Hi all,

I'm trying to run a Story Runner integration test that uploads a file
through Attachment-fu.

I've tried various ways of specifying the file data, from custom mocks:
class MockFile < Struct.new
(:original_filename, :read, :content_type); end
fdata = MockFile.new  "test_upload.txt", "Test Upload", "text/plain"

to 'proper' railsy stubs like:

fdata = ActionController::TestUploadedFile.new("../spec/fixtures/
test_upload.txt", "text/plain")

The actual params are specified as:
{ :uploaded_data => fdata,
     :filename => file_reference_name(identifier)
   }

Now, this is being passed successfully into attachment-fu, where it
is processed. Well, almost successfully. Somehow, along the way, the
TestUploadedFile, struct, mock, whatever you want, gets turned into a
String. Then it blows up when attachment-fu tries to retrieve the
content type:

#<NoMethodError: undefined method `content_type' for
"#<ActionController::TestUploadedFile:0x34349ac>":String>

Somehow, I can't seem to pass anything but a String down into the
controller. Even in the action, the TestUploadedFile is already
transmogrified into a String (which, of course, has no content_type
property). I put a logging statement in the ActionController too, and
it seems that the entire params hash is turned into a string
somewhere in there - which would explain why the object has literally
no chance of going through.

I'm a bit stumped by this. I guess it makes sense that params are
turned into strings - but then what sort of string should i specify
for a file upload?

Thanks for any help.

Daniel
Posted by David Chelimsky (Guest)
on 12.12.2007 19:00
(Received via mailing list)
On Dec 11, 2007 6:18 AM, Daniel Tenner <daniel.ruby@tenner.org> wrote:
> to 'proper' railsy stubs like:
> is processed. Well, almost successfully. Somehow, along the way, the
> property). I put a logging statement in the ActionController too, and
> it seems that the entire params hash is turned into a string
> somewhere in there - which would explain why the object has literally
> no chance of going through.
>
> I'm a bit stumped by this. I guess it makes sense that params are
> turned into strings - but then what sort of string should i specify
> for a file upload?
>
> Thanks for any help.

Code please.
Posted by Daniel Tenner (Guest)
on 12.12.2007 19:49
(Received via mailing list)
I've pasted up the code at:

http://pastie.caboo.se/126925

Since there are quite a few files involved.

Thanks for your time,

Daniel
Posted by Luis Lavena (luislavena)
on 12.12.2007 20:00
(Received via mailing list)
On Dec 11, 2007 10:57 AM, Daniel Tenner <daniel.ruby@tenner.org> wrote:
>
> I've pasted up the code at:
>
> http://pastie.caboo.se/126925
>
> Since there are quite a few files involved.
>
> Thanks for your time,
>

A similar discussion was raised back in november (about 
fixture_file_upload):

http://rubyforge.org/pipermail/rspec-users/2007-November/004378.html

Instead of mocking the FileUpload, why not provide a real one?

After all, the StoryRunner is aimed to fully exercise your code :-D

Also, adjust the path of your TestFileUpload file location, instead of
a relative one (../spec/fixtures) provide one related to RAILS_ROOT,
which will work no matter where you put your helper or what is your
current directory (Dir.chdir).

HTH,

--
Luis Lavena
Multimedia systems
-
A common mistake that people make when trying to design
something completely foolproof is to underestimate
the ingenuity of complete fools.
Douglas Adams
Posted by Daniel Tenner (Guest)
on 12.12.2007 20:00
(Received via mailing list)
Hi Luis,

I read through that thread, but unfortunately it wasn't much help.

fixture_file_upload also creates an
ActionController::TestUploadedFile. It's is just a shortcut to
ActionController::TestUploadedFile.new.

There's no problem with the path - it creates the fixture and loads
the file, and has the right values inside if I inspect the params
directly before posting them:

{:filename=>"file-
TFile.txt", :uploaded_data=>#<ActionController::TestUploadedFile:
0x33f83bc @content_type=#<Mime::Type:0x10e2490 @symbol=:text,
@synonyms=[], @string="text/plain">, @tempfile=#<File:/tmp/
test_upload29049-0.txt>, @original_filename="test_upload.txt">}

It's only once this is passed into the controller that it all gets
to_s'ed.

I like the idea of 'providing a real file upload', but I'm not sure
how to do that in a Story... if anyone has any suggestions or
pointers, they would be welcome!

Thanks,

Daniel
Posted by Daniel Tenner (Guest)
on 12.12.2007 20:31
(Received via mailing list)
I've now located where this is going wrong, though I am not yet sure
how to fix it.

In rails' integration.rb, there is a method called 'process'. around
line 226. It mangles the parameters by calling:
           data = requestify(parameters)

doing:
           puts "\n1----===#{parameters.inspect}"
           puts "\n2----===#{data.inspect}"

a bit later reveals the problem:
1----==={:quick_file=>{:filename=>"file-
TFile.txt", :uploaded_data=>#<ActionController::TestUploadedFile:
0x3434150 @content_type="text/plain", @tempfile=#<File:/tmp/
test_upload29259-0.txt>,
@original_filename="test_upload.txt">}, :quick_folder=>
{:id=>1330}, :request_type=>"xml"}

2----==="quick_file%5Bfilename%5D=file-TFile.txt&quick_file%
5Buploaded_data%5D=%23%3CActionController%3A%3ATestUploadedFile%
3A0x3434150%3E&quick_folder%5Bid%5D=1330&request_type=xml"

Obviously that's not going to work. I wonder how Test::Unit does it,
then, though...

Daniel
Posted by Raimond Garcia (voodoorai2000)
on 12.12.2007 20:51
(Received via mailing list)
Hi,

I had the same problem, couldn't get it to run with Stories thought...
Had to test file uploads using controller specs, which works but is not 
as
clear as a Story.

I'm guessing the bug where it changes the TempFile to a String will be 
fixed
soon by the core team, but if someone has a patch it would be awesome!


Rai



Daniel Tenner wrote:
>            puts "\n2----===#{data.inspect}"
> 5Buploaded_data%5D=%23%3CActionController%3A%3ATestUploadedFile% 
>>
>> {:filename=>"file- 
>> pointers, they would be welcome!
>>>> I've pasted up the code at:
>>>
>>> current directory (Dir.chdir).
>>> Douglas Adams
> 
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
> 

--
View this message in context: 
http://www.nabble.com/Attachment-fu-%2B-Story-Runner-tp14272891p14291797.html
Sent from the rspec-users mailing list archive at Nabble.com.
Posted by Daniel Tenner (Guest)
on 12.12.2007 21:05
(Received via mailing list)
Ok, a bit more digging and I found this is a Rails issue, that is
logged here:

http://dev.rubyonrails.org/ticket/4635

File uploads don't work in Rails integration tests either, only in
functional tests. This is not specific to Attachment-Fu.

There is, however, a monkey patch that you can download to enable a
multipart_post method which will not mangle the params anymore.

Hope this helps others too!

Daniel
Posted by David Chelimsky (Guest)
on 12.12.2007 21:06
(Received via mailing list)
On Dec 12, 2007 5:41 AM, Daniel Tenner <daniel.ruby@tenner.org> wrote:
> multipart_post method which will not mangle the params anymore.
>
> Hope this helps others too!

Daniel - thanks for doing the research on this. There are no tests for
the rails patch. If you're willing to add rspec examples and
contribute it to the rspec tracker, I'll add it to rspec as a
temporary fix until that ticket is resolved.
Posted by Joe Van Dyk (Guest)
on 20.03.2008 05:38
(Received via mailing list)
What was the resolution on this?  How do you people test uploads with
rspec stories?
Posted by Rick Denatale (rdenatale)
on 20.03.2008 11:41
(Received via mailing list)
On 3/20/08, Joe Van Dyk <joe@pinkpucker.net> wrote:
> What was the resolution on this?  How do you people test uploads with
>  rspec stories?

I submitted a Rails patch to address the underlying problem
http://dev.rubyonrails.org/ticket/11091

It got in as changeset 8978 and should be in edge rails.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
Posted by Joe Van Dyk (Guest)
on 21.03.2008 07:44
(Received via mailing list)
On Thu, Mar 20, 2008 at 3:34 AM, Rick DeNatale <rick.denatale@gmail.com> 
wrote:
> On 3/20/08, Joe Van Dyk <joe@pinkpucker.net> wrote:
>  > What was the resolution on this?  How do you people test uploads with
>  >  rspec stories?
>
>  I submitted a Rails patch to address the underlying problem
>  http://dev.rubyonrails.org/ticket/11091
>
>  It got in as changeset 8978 and should be in edge rails.

Would it be worth it to monkeypatch that patch into rspec?
Posted by David Chelimsky (Guest)
on 21.03.2008 13:47
(Received via mailing list)
On Fri, Mar 21, 2008 at 1:35 AM, Joe Van Dyk <joe@pinkpucker.net> wrote:
>  Would it be worth it to monkeypatch that patch into rspec?
Why would we do that? It's already in edge rails.
Posted by Joe Van Dyk (Guest)
on 05.04.2008 01:13
(Received via mailing list)
On Fri, Mar 21, 2008 at 5:46 AM, David Chelimsky <dchelimsky@gmail.com> 
wrote:
>  >  >  It got in as changeset 8978 and should be in edge rails.
>  >
>  >  Would it be worth it to monkeypatch that patch into rspec?
>
>  Why would we do that? It's already in edge rails.

Because I can't upgrade to edge rails.  And I want to be able to test
file uploads.

I've applied the 8978 changeset to my stable rails -- but I'm still
unsure of how to test file uploads in the context of a rspec story.
Normally, I'd use fixture_file_upload, but that method doesn't seem to
be available in rspec.  Do I need to do anything special?

Is anyone testing file uploads with rspec stories?
Posted by Joe Van Dyk (Guest)
on 05.04.2008 01:41
(Received via mailing list)
On Fri, Apr 4, 2008 at 4:10 PM, Joe Van Dyk <joe@pinkpucker.net> wrote:
>  >  >  >
>  unsure of how to test file uploads in the context of a rspec story.
>  Normally, I'd use fixture_file_upload, but that method doesn't seem to
>  be available in rspec.  Do I need to do anything special?
>
>  Is anyone testing file uploads with rspec stories?

Think I got it working, here's an example step:

  When("$username uploads a picathon image") do |username|
    params = { :media =>
ActionController::TestUploadedFile.new(RAILS_ROOT +
'/test/fixtures/files/image.jpg') }
    @sessions[username].post user_uploads_path(@users[username]), params
    @sessions[username].visits user_uploads_path(@users[username])
  end