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
image.
The code it’s quite simple and stupid:

errors.add(:image, ‘at least one’) if post.images.size.zero?

…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.new
post.should_not be_valid
end

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

end

using Test::Unit

class PostTest < Test::Unit::TestCase

def test_invalid_without_at_least_one_image
post = Post.new
assert !post.valid?
end

def test_valid_with_at_least_one_image
post = Post.new :images => [Image.new]
assert post.valid?
end

end

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”

Regards,
Craig

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