I'm having a problems mocking successive return values.  I don't know if I'm
doing something wrong or if this is a limitation of rspec mocks.  Any ideas
of what I may be doing wrong?

I'm trying to test the generate_quote_number method.  It generates a quote
number, looks to see if it is in the db already.  If it is, it calls itself
to try again.  I want to test that it looks in the database for matching
numbers until it finds one which does not exist already.

## The code
 def generate_quote_number
    quote_num = "MMQ#{self.random_string}-001"
    if Policy.find(:first, :conditions => ["quote_number = ?", quote_num])
      quote_num = generate_quote_number
    end
    quote_num
  end

  def random_string(size=8)
     # return a random string
  end


For the test, I simply lookup 3 existing quote numbers, append nil to the
end (for a total of 4)
## The test
  it "should not generate a duplicate quote number" do
    existing_quote_numbers = Policy.find(:all).map{|p| p.quote_number}[0,3]

@policy_service.should_receive(:random_string).with(:no_args).exactly(4).times.and_return(existing_quote_numbers.concat([nil]))
    @policy_service.generate_quote_number
  end

##  The result
should not generate a duplicate quote number
Mock 'Service::Base' expected :random_string with (no args) 4 times, but
received it once

I verified "and_return" is actually returning the array rather than an
individual element of the existing_quote_numbers array which makes sense,
but what about this?
http://rspec.rubyforge.org/svn/branches/dogfood/spec/spec/mocks/mock_spec.rb

specify "should use a list of return values for successive calls" do
        @mock.should_receive(:multi_call).twice.with(:no_args).and_return([8,
12])
        @mock.multi_call.should_equal 8
        @mock.multi_call.should_equal 12
        @mock.__verify
end

Any feedback on how to properly test this is much appreciated.

Thanks,
Doug
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to