[rspec-users] How to spec an attachment_fu model

Matt Lins mlins at webinforem.com
Wed Aug 22 12:23:48 EDT 2007


First off, I'm not trying to spec attachment_fu, I know it's been tested.

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.  
Here it is:

      # This method handles the uploaded file object.  If you set the 
field name to uploaded_data, you don't need
      # any special code in your controller.
      #
      #   <% form_for :attachment, :html => { :multipart => true } do 
|f| -%>
      #     <p><%= f.file_field :uploaded_data %></p>
      #     <p><%= submit_tag :Save %>
      #   <% end -%>
      #
      #   @attachment = Attachment.create! params[:attachment]
      #
      # TODO: Allow it to work with Merb tempfiles too.
      def uploaded_data=(file_data)
        return nil if file_data.nil? || file_data.size == 0
        self.content_type = file_data.content_type
        self.filename     = file_data.original_filename if 
respond_to?(:filename)
        if file_data.is_a?(StringIO)
          file_data.rewind
          self.temp_data = file_data.read
        else
          self.temp_path = file_data.path
        end
      end

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.

How can I create a valid attachment_fu model spec(so I can start 
tweaking it to test my other validations)?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070822/c286d77d/attachment.html 


More information about the rspec-users mailing list