[rspec-users] Newbie: lambda do...end.should change(Model, :count).by(1). Doesn't work

Rick DeNatale rick.denatale at gmail.com
Sat May 10 10:28:55 EDT 2008

On Sat, May 10, 2008 at 9:40 AM, Craig Demyanovich
<cdemyanovich at gmail.com> wrote:
> A syntax note from the RSpec docs: "blocks passed to should change and
> should_not change must use the {} form (do/end is not supported)."
> http://rspec.info/rdoc/classes/Spec/Matchers.html#M000386

But, I'm pretty sure that this is talking about a block which is
passed as an argument to should_change/should_not_change as in this
example from the docs:

string = "string"
  lambda {
  }.should change { string }.from("string").to("gnirts")

as opposed to:

string = "string"
  lambda {
  }.should change do

This is because do/end binds less tightly than {} so that in the first
(working case) the block is passed to the change method, while in the
second one it gets passed to the should method instead.

On the other hand:

string = "string"
  lambda do
  end.should change { string }.from("string").to("gnirts")

should work, although I prefer the {} syntax in this case.
Rick DeNatale

My blog on Ruby

More information about the rspec-users mailing list