[rspec-users] test for "lambda {...}.should change(x, y).by(z)" failing.. ?

Patrick J. Collins patrick at collinatorstudios.com
Thu Nov 24 15:50:18 EST 2011


> As you can see, @seller.reload is only evaluated once, and its reputation
> score is going to be the same both times.

Aha..  Makes perfect sense.  Thanks.

> Tangent: this is testing two things - @seller.reputation_score and
> Event.reputation_change_for(:mark_helpful). If either is failing to work
> correctly, this example won't tell you which. I'd recommend sticking to
> literals in expectations:
>
>   lambda { @comment.update_attribute(:helpful, true) }.  should change
>   {@seller.reload.reputation_score }.by(3)

Hmmm..  I totally get why you say this, but part of me really hates the idea of
that 3 someday changing to 5 and then my test breaking, forcing me to update
the 3 in multiple places.

Would it not be safe to assume that if there's a test for Event.rb verifying
the behavior of Event.reputation_change_for, then it's safe to use that in a
example?  When I originally wrote this, I wanted to stub out the Event::SCORES
constant and return a hash with just something like:
  { :mark_helpful => { :reputation_change => 123 } }

But I couldn't figure out how to stub a constant.....  Of course I could just
make my test overwrite the mark helpful value of that constant like:

Event:SCORES[:mark_helpful] = { :reputation_change => 123 }

But that felt a little wrong so I chose to just rely on a class convenience
method to return the constant's value.

Any thoughts on these other approaches?

Patrick J. Collins
http://collinatorstudios.com


More information about the rspec-users mailing list