# Rspec unit test fails sometimes (validation race condition)

**URL:** <https://www.ruby-forum.com/t/rspec-unit-test-fails-sometimes-validation-race-condition/189117>\
**Category:** Rails\
**Created:** [May 25, 2010, 12:50am UTC](https://www.ruby-forum.com/t/rspec-unit-test-fails-sometimes-validation-race-condition/189117 "2010-05-25T00:50:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [May 25, 2010, 12:50am UTC](https://www.ruby-forum.com/t/rspec-unit-test-fails-sometimes-validation-race-condition/189117/1 "2010-05-25T00:50:51Z")

</div>

I have code similar to this:

class Post  
has\_many :comments  
end

class Comment  
belongs\_to :post  
validate :must\_not\_exceed\_ten

def must\_not\_exceed\_ten  
errors.add\_to\_base(‘Too many comments’) unless post.comments.length  
\< 10  
end  
end

and in the specs (Rspec + machinist):

post = Post.make  
9.times { Comment.make(:post =\> post) }  
comment = Comment.new(:post =\> post)  
comment.save.should be\_false

The weird thing about it is that it fails sometimes! Especially if I run  
the whole test suite with rake command (the probability is higher). When  
I check in the console post indeed has 11 comments. Can somebody give me  
a reasonable explanation why this is happening? Is this some sort of a  
race condition that I’m missing?

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [May 25, 2010, 12:54am UTC](https://www.ruby-forum.com/t/rspec-unit-test-fails-sometimes-validation-race-condition/189117/2 "2010-05-25T00:54:25Z")

</div>

In the specs it is actually 10.times instead of 9.

Milan D. wrote:

> post = Post.make  
> 9.times { Comment.make(:post =\> post) }  
> comment = Comment.new(:post =\> post)  
> comment.save.should be\_false

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [May 25, 2010, 1:08am UTC](https://www.ruby-forum.com/t/rspec-unit-test-fails-sometimes-validation-race-condition/189117/3 "2010-05-25T01:08:09Z")

</div>

Milan D. wrote:

> In the specs it is actually 10.times instead of 9.
> 
> Milan D. wrote:
> 
> > post = Post.make  
> > 9.times { Comment.make(:post =\> post) }  
> > comment = Comment.new(:post =\> post)  
> > comment.save.should be\_false

Well, there’s your problem, then – you’ve already made your 10th  
comment!

BTW, you should consider comments.count instead of .length – let the DB  
do the counting.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [May 25, 2010, 1:16am UTC](https://www.ruby-forum.com/t/rspec-unit-test-fails-sometimes-validation-race-condition/189117/4 "2010-05-25T01:16:17Z")

</div>

No because validations happen before the object is saved. Validation  
will pass and the object will get saved properly. Well… sometimes.  
Sometimes not.

Marnen Laibow-Koser wrote:

> Well, there’s your problem, then – you’ve already made your 10th  
> comment!
> 
> BTW, you should consider comments.count instead of .length – let the DB  
> do the counting.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid
