[rspec-users] Assertions for asynchronous behaviour

Alex Chaffee
Wed, 21 Sep 2011 16:46:36 -0000

On Wed, Sep 21, 2011 at 3:42 AM, Matt Wynne <matt at mattwynne.net>
wrote:
> I''ve never used Wrong, only read about it--and I like the idea
very much, I must say. Is there any danger of adverse effects (other than an
extra line the Gemfile) if we have to use the Wrong RSpec adapter in the book
alongside the existing vanilla RSpec assertions?
>

There shouldn''t be, but I''d love for more people to verify
that. The
adapter source is at

https://github.com/sconover/wrong/blob/master/lib/wrong/adapters/rspec.rb

(It doesn''t actually do much, just includes Wrong inside
RSpec''s
ExampleGroup. The bulk of that code is in case anyone uses the Wrong
feature "alias_assert" which allows users to define their own DSLy
name for assert. I like "expect" but RSpec already has its own
"expect"; David kindly provided a way to cleanly remove
RSpec''s
"expect" before defining my own, but I couldn''t quite get it
to work
so I''m still using the brute force way which just chops it out using
Ruby''s "Module#remove_method" method. In any case, none of
that is
relevant unless users go out of their way and call
"Wrong.config.alias_assert :expect, :override=>true" which most
won''t
do.)

There''s a semantic issue in your eventually method that I''d
like to
discuss. My wait_for[1] and friends take a *predicate* (in the form of
a block) and wait for it to return true(ish). Your eventually[2]
ignores the return condition, and merely waits for it to not raise an
error. I think using a predicate is more useful, and strictly no less
powerful since the waiter code will *also* wait for it to not raise an
error, so you could do *either* this

    eventually { rand(10) == 0 }

or this

    eventually { rand(10).should == 0 }

and if, after 2 sec or whatever, it keeps either being false or
raising an ExpectationNotMetError, then the waiter will itself raise
an ExpectationNotMetError.

IOW, should the following pass or should it fail?

    eventually { false == true }

 - A

P.S. (Lurkers please feel free to chime in too. :-)

[1] https://github.com/pivotal/selenium/blob/master/lib/selenium/wait_for.rb
[2] https://gist.github.com/1228927

--
Alex Chaffee - alex at stinky.com