2009-07-10 35 views
0

我正在创建一个程序,它将检查由用户登录的图像。我有RMagick代码写入做检查(基本上发现如果一个像素黑色在图像中),但我不知道如何为这个模型编写单元测试。使用附件的功能的轨道单元测试

目前我正在使用回形针将上传的文件附加到模型上,我知道使用数据库中的多个字段来跟踪文件。我应该如何设置我的灯具,以便每次都能对相同的数据进行单元测试?

我的模型是目前:

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 

回答

0

最好在你的功能测试

单元测试上的fixture_file_upload使用读了,我通常有删除这些文件一旦完成,他们一个拆卸方法已被修改(虽然它可用于复制原件,无论您想要什么 - 请参阅fileutils库)

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