Search code examples
rspecexpectations

How to specify expected return values?


I'm new to RSpec and as I was writing a spec test I came across a problem where the spec tests are passing even though the return values are different than what I specified in my expectations. For example:

  @q= Query.new
  @q.should_receive(:number_to_name).with(0).and_return("no such boro") 
  @q.number_to_name(0) 

This passes even though the Query.number_to_name is returning a different value when I call it with 0. I don't understand what to make of this.


Solution

  • should_receive is part of rspec's mocking tools.

    @q.should_receive(:number_to_name).with(0).and_return("no such boro")
    

    You are mocking number_to_name on @q, setting an expectation that number_to_name will be called with an argument of 0 and that the mock will return "no such boro". Executing @q.number_to_name(0) then satisfies that expectation by calling the mock. Your implementation of number_to_name has never been called and is not being tested.