Forum: Ruby on Rails Testing acts_as_attachment?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-07 10:06
Since attachment_fu isn’t working for me at the moment, I’ve switched to
acts_as_attachment.

Does anyone know how to set up a simple unit test for
acts_as_attachment? I’ve ventured into the included tests but they’re a
bit over my head, to be honest.

Is it possible to use fixtures to test aaa?

For example, suppose I have a simple model called Picture, which has
just the basic acts_as_attachment fields. How would I set up some simple
unit tests to make sure it’s working? Can I use fixtures or do I need to
use methods in my unit tests to use fixture_file_upload?

I’m wondering if anyone has any straightforward examples for a very
simple case that I could learn from. Thanks!

Jeff
34d628e9573784a90ce8806f3e85ce0a?d=identicon&s=25 Jeroen Houben (jeroen)
on 2007-03-11 17:45
(Received via mailing list)
Hi,

I found this test helper on the Rails wiki, works like a charm.

  # returns an uploaded screenshot you can use to test file uploads in
your controllers
  def uploaded_screenshot
    uploaded_file("#{File.expand_path(RAILS_ROOT)}/test/fixtures/
samplescreenshot.jpg", "image/jpeg")
  end

  # returns an uploaded movie you can use to test file uploads in your
controllers
  def uploaded_movie
    uploaded_file("#{File.expand_path(RAILS_ROOT)}/test/fixtures/
samplemovie.flv", "video/flv")
  end

  # get us an object that represents an uploaded file
  def uploaded_file(path, content_type="application/octet-stream",
filename=nil)
    filename ||= File.basename(path)
    t = Tempfile.new(filename)
    FileUtils.copy_file(path, t.path)
    (class << t; self; end;).class_eval do
      alias local_path path
      define_method(:original_filename) { filename }
      define_method(:content_type) { content_type }
    end
    return t
  end

put that in test_helper.rb

Then in your controller test, put this:

  # update a fragment
  # uses test helper uploaded_screenshot() to simulate a file upload
  def test_should_update_fragment
    put :update, :id => 1,  :fragment   => { :title => 'new f' },
                                     :screenshot => { :uploaded_data
=> uploaded_screenshot }

    assert_redirected_to fragment_path(assigns(:fragment)),
assigns(:fragment).errors.inspect
  end





On Mar 7, 10:06 am, Jeff Coleman <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.