Forum: Ruby Problem with rspec mock object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-05-16 04:52
(Received via mailing list)
I have perhaps the simplest mock object ever - it doesn't do anything.
 However when I use it, it gives me an error.  Here's the full code,
along with the error:

require "rental"

context "A new rental" do
  setup do
    @movie = mock("Movie")
    @rental = Rental.new(@movie, 3)
  end

  specify "should have a movie and rental length" do
    @rental.movie.should_equal @movie
    @rental.days_rented.should_be 3
  end
end

class Rental
  attr_reader :movie, :days_rented

  def initialize(movie, days_rented)
    @movie = movie
    @days_rented = days_rented
  end

  def get_charge
    movie.get_charge days_rented
  end

  def get_frequent_renter_points
    movie.get_frequent_renter_points days_rented
  end
end

MockExpectationError in 'A new rental should have a movie and rental
length'
Mock 'Movie' received unexpected message 'to_s' with []
./specs/rental_spec.rb:10:in `should have a movie and rental length'

So when I'm calling @rental.movie.should_equal @movie, it's blowing
up.  I have no clue what's going on.  After doing some googling and
looking at the source, all I know is that to_s is undefined on my mock
object - I don't know why it's being called though.

Pat
This topic is locked and can not be replied to.