# Rspec / Duplicate Errors for same object

**URL:** <https://www.ruby-forum.com/t/rspec-duplicate-errors-for-same-object/120408>\
**Category:** Rails\
**Created:** [November 16, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/rspec-duplicate-errors-for-same-object/120408 "2007-11-16T17:25:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [November 16, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/rspec-duplicate-errors-for-same-object/120408/1 "2007-11-16T17:25:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [November 16, 2007, 5:34pm UTC](https://www.ruby-forum.com/t/rspec-duplicate-errors-for-same-object/120408/2 "2007-11-16T17:34:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [November 16, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/rspec-duplicate-errors-for-same-object/120408/3 "2007-11-16T18:23:03Z")

</div>

I forgot to note this is on Edge Rails, and trunk of Rspec.

-Dave

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [November 17, 2007, 12:53am UTC](https://www.ruby-forum.com/t/rspec-duplicate-errors-for-same-object/120408/4 "2007-11-17T00:53:10Z")

</div>

On Nov 17, 2007 3:24 AM, Dave [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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… 😉

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [November 19, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/rspec-duplicate-errors-for-same-object/120408/5 "2007-11-19T16:23:28Z")

</div>

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:

1. 

‘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 M.
