Ruby Forum Ruby on Rails > Rspec / Duplicate Errors for same object

Posted by Dave (Guest)
on 16.11.2007 17:25
(Received via mailing list)
Hi there,

I'm getting duplicate errors when I should only be receiving one error
in the @errors array.

I have an Rspec test that looks like this:

describe ImageCollection, "validations" do
  it "should require max_height to be numeric" do
    collection = ImageCollection.create(:max_height => "xxx")
    puts "Errors: #{collection.errors.inspect}"
    collection.should have(1).error_on(:max_height)
  end

  it "should require max_height to be numeric" do
    collection = ImageCollection.create(:max_width => "xxx")
    puts "Errors: #{collection.errors.inspect}"
    collection.should have(1).error_on(:max_width)
  end
end


class ImageCollection < ActiveRecord::Base
   validates_numericality_of :max_height, :max_width
end

Nothing too fancy about that setup. When I run 'autotest' and print
out what those errors are I see duplicate errors in the array for the
same attribute.

---
Errors: #<ActiveRecord::Errors:0x3f0c780 @base=#<ImageCollection id:
nil, page_type: nil, name: nil, created_at: nil, updated_at: nil,
page_id: nil, max_height: nil, max_width: 0, label: nil, type: nil,
is_active: false>, @errors={"max_width"=>[["is not a number", nil],
["is not a number", nil]], "max_height"=>[["is not a number", nil],
["is not a number", nil]]}>

Errors: #<ActiveRecord::Errors:0x3f05d68 @base=#<ImageCollection id:
nil, page_type: nil, name: nil, created_at: nil, updated_at: nil,
page_id: nil, max_height: 0, max_width: nil, label: nil, type: nil,
is_active: false>, @errors={"max_width"=>[["is not a number", nil],
["is not a number", nil]], "max_height"=>[["is not a number", nil],
["is not a number", nil]]}>
---

Notice how the 'max_height' attribute errors array has 2 entries for
"is not a number". I'm only expecting 1 error, but I'm getting 2. Same
goes for 'max_height'.

Has anyone experienced these types of 'bugs'/'errors' before? Maybe
I'm missing something obvious here.

Thoughts?

Thanks!
-Dave
Posted by Thorsten Mueller (thorsten)
on 16.11.2007 17:34
the second function should be
it "should require max_width to be numeric" do

instead of repeating:
it "should require max_height to be numeric" do

that's something rspec does not like very much
Posted by Dave (Guest)
on 16.11.2007 18:23
(Received via mailing list)
I forgot to note this is on Edge Rails, and trunk of Rspec.

-Dave
Posted by George (Guest)
on 17.11.2007 00:53
(Received via mailing list)
On Nov 17, 2007 3:24 AM, Dave <dhoefler@gmail.com> wrote:
> end
>
> Has anyone experienced these types of 'bugs'/'errors' before? Maybe
> I'm missing something obvious here.

I don't know if it's the cause, but you probably don't want two specs
named the same thing here.


> Notice how the 'max_height' attribute errors array has 2 entries for
> "is not a number". I'm only expecting 1 error, but I'm getting 2. Same
> goes for 'max_height'.

And there's a similar bug in the above paragraph... ;-)
Posted by Dave (Guest)
on 19.11.2007 16:23
(Received via mailing list)
I changed the specs to be named something different, but that still
didn't fix it.

describe ImageCollection, "validations" do
  it "should require max_height to be numeric" do
    collection = ImageCollection.create(:max_height => nil, :max_width
=> 100, :is_active => false)
    puts "Errors: #{collection.errors.inspect}"
    collection.should have(1).error_on(:max_height)
  end

  it "should require max_width to be numeric" do
    collection = ImageCollection.create(:max_width => nil, :max_height
=> 100, :is_active => false)
    puts "Errors: #{collection.errors.inspect}"
    collection.should have(1).error_on(:max_width)
  end
end

I'm still getting duplicate errors.

1)
'ImageCollection validations should require max_width to be numeric' 
FAILED
expected 1 error on :max_width, got 2
./spec/models/image_collection_spec.rb:32:
script/spec:4:

2)
'ImageCollection validations should require max_height to be numeric' 
FAILED
expected 1 error on :max_height, got 2
./spec/models/image_collection_spec.rb:26:
script/spec:4:


One of the other guys that I work with checked out the project on his
Red Hat box and all the tests pass. I went to check out a fresh copy
on Leopard and I get these 2 errors. How odd!


Thanks,
Dave

On Nov 16, 2007 10:34 AM, Thorsten Muller