unit test in rails – model with paperclip

Adding file to a model is dead simple. For example:

@post = Post.new
@post.attachment = File.new("test/fixtures/sample_file.png")
# Replace attachment= with the name of your paperclip attachment

In that case you should put the file into your test/fixtures dir.

I usually make a little helper in my test_helper.rb

def sample_file(filename = "sample_file.png")
  File.new("test/fixtures/#{filename}")
end

Then

@post.attachment = sample_file("filename.txt")

If you use something like Factory Girl instead of fixtures this becomes even easier.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)