Monday, May 24, 2010

[Rails] Rspec unit test fails sometimes (validation race condition)

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?
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate