Attachment_fu, how would you test this?

Hi, i’m starting to go nuts because of this. i’m playing with
attachment_fu, everything is ok, but i also want to put everything under
test too. Actually i’ve a post, where you can upload images, and it
works. But now i want to set that it’s mandatory to upload at least 1
The code it’s quite simple and stupid:

errors.add(:image, ‘at least one’) if

…and obviously it works…

…but how would you test this by unit tests?

Here’s a rough cut off the top of my head. (I haven’t used Test::Unit
in a long time; I hope I’m not too rusty).

using RSpec

describe Post do

it “is invalid without at least one image” do
post =
post.should_not be_valid

it “is valid with at least one image” do
post = :images => []
post.should be_valid


using Test::Unit

class PostTest < Test::Unit::TestCase

def test_invalid_without_at_least_one_image
post =
assert !post.valid?

def test_valid_with_at_least_one_image
post = :images => []
assert post.valid?


I assume that you’ve written your own validate method on your model.
You can also use validates_length_of or validates_size_of for the
images association, e.g.,

validates_length_of :images, :minimum => 1, :message => “at least %d”


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs