[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:

#spec_reader.rb
describe Reader do
 it "should stop reading items when called with a limit" do
  @stack = mock(Stack)
  Stack.should_receive(:new).and_return(@stack)
 
@stack.should_receive(:read).exactly(3).times.and_return(2,4,1,5,3,1,false)
 
  @reader = Reader.new
  @reader.read_stack(3)
 end
end

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

 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
   ...
  end
end

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