<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
<font size="-1"><font face="Arial">First off, I'm not trying to spec
attachment_fu, I know it's been tested.<br>
<br>
But, I added some code to that model that I do need to test. Basically,
I need to somehow fulfill the "uploaded_data" property so I can
actually run my tests(otherwise they fail because of validations). The
"uploaded_data" field is what would grab the multipart data from form.&nbsp;
Here it is:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # This method handles the uploaded file object.&nbsp; If you set the
field name to uploaded_data, you don't need<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # any special code in your controller.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp; &lt;% form_for :attachment, :html =&gt; { :multipart =&gt;
true } do |f| -%&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; &lt;p&gt;&lt;%= f.file_field :uploaded_data %&gt;&lt;/p&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; &lt;p&gt;&lt;%= submit_tag :Save %&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp; &lt;% end -%&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp; @attachment = Attachment.create! params[:attachment]<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # TODO: Allow it to work with Merb tempfiles too.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def uploaded_data=(file_data)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return nil if file_data.nil? || file_data.size == 0 <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.content_type = file_data.content_type<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.filename&nbsp;&nbsp;&nbsp;&nbsp; = file_data.original_filename if
respond_to?(:filename)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if file_data.is_a?(StringIO)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file_data.rewind<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.temp_data = file_data.read<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.temp_path = file_data.path<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br>
<br>
While I was digging I found out that "file_data" is a ruby Tempfile
object. So, I tried to create a dummy file and pass it, but it failed.
I found out in the CGI library, it actually extends the Tempfile object
and adds some methods/properties. It started to get messy, so I thought
I'd ask for advice.<br>
<br>
How can I create a valid attachment_fu model spec(so I can start
tweaking it to test my other validations)?<br>
</font></font>
</body>
</html>