[rspec-users] Can't use #exactly when #and_return influences it.

pangel pangel.neu at gmail.com
Thu Nov 1 06:26:45 EDT 2007

I want my class Reader to loop on the content of its Stack until false is
returned. I also want an optional argument to exist that limits how many
times the stack will be read, even if some elements are left. I was trying
to spec this last bit but ended up on a false positive.

Hope the example will be clear enough:

describe Reader do
 it "should stop reading items when called with a limit" do
  @stack = mock(Stack)
  @reader = Reader.new

# reader.rb
class Reader
 def initialize
  @stack = Stack.new

 def read_stack(limit = 0) #0 = read until false is returned
  while val = @stack.read
   # Not implemented on purpose!
   # return if limit == 0
   # limit -= limit

So the test should fail, because the use of the limit argument has not been
implemented yet and #read gets called 7 times. But the test actually passes.
I looked into it a bit and it seems that number of arguments in #and_return
overrides the arguments of #exactly.

Did I misunderstand the point of #exactly or should I build my code
differently so that the value returned by #and_return does not influence how
many times #read is called?

View this message in context: http://www.nabble.com/Can%27t-use--exactly-when--and_return-influences-it.-tf4730412.html#a13526196
Sent from the rspec-users mailing list archive at Nabble.com.

More information about the rspec-users mailing list