Is it possible to chain two matchers and only yield the block given to expect once?

15 views
Skip to first unread message

Kris Leech

unread,
Aug 15, 2014, 12:49:47 PM8/15/14
to rs...@googlegroups.com
Is it possible to chain two matchers and not have to yield the block given to expect twice?

The problem is yielding the block twice changes the test.

class FirstMatcher
 
def matches?(block)
   
@result = record_something do
      block
.call # block called for first time
   
end
   
return true
 
end

 
def supports_block_expectations?
   
true
 
end

 
def second_matcher?
   
SecondMatcher.new
 
end
end

class SecondMatcher
 
def matches?(block)
   
@result = record_something do
      block
.call # block called for second time, can I get the original @result somehow?
   
end
   
return true
 
end

 
def supports_block_expectations?
   
true
 
end
end


# usage

expect
{ something }.to first_matcher.second_matcher

Many thanks, Kris.

Myron Marston

unread,
Aug 15, 2014, 4:39:03 PM8/15/14
to rs...@googlegroups.com
In RSpec 3.0, there's not.  In 3.1, there will be:


Use the `and` chaining:

expect { }.to first_matcher.and second_matcher

...and it should just work.

Myron

Kris Leech

unread,
Aug 15, 2014, 6:31:12 PM8/15/14
to rs...@googlegroups.com
Can't wait, I'll keep my eyes open for a 3.1 beta. Really enjoying Rspec 3.

Actually I can just point at the master branch and try it out now.

Many thanks.
Reply all
Reply to author
Forward
0 new messages