views:

68

answers:

1

I'm creating a program which will be examining images which are uploaded by the users logged in. I have the RMagick code written up to do the examination (basically finding out if a pixel black is in an image), but I don't know how to write the unit tests for this model.

Currently I'm using paperclip to attach the uploaded file to the model, which I understand uses a number of fields in the database for tracking the files. How should I set up my fixtures so that I can do unit testing on the same data every time?

My model is currently:

class Map < ActiveRecord::Base
  has_attached_file :image, :styles => { :small => "150x150>" }
  validates_attachment_presence :image
  validates_uniqueness_of :name, :message => "must be unique"

  def pixel_is_black(x, y)
    <code to return true if position (x,y) in image is black>
  end
end
A: 

Best to read up on the usage of fixture_file_upload in your functional tests

For unit tests, i normally have a teardown method that deletes the files once done they have been modified (though it can be used to copy over originals, whatever you want - see the fileutils library for that)

def setup
  FileUtils.cp 'original.jpg', '/path/to/where/file/exists/in/fixture.jpg'
end

def teardown
  Fileutils.rm '/path/to/where/file/exists/in/fixture.jpg', :force => true
end
Omar Qureshi