¿Cómo especificar los valores de retorno esperados?

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") 

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.

preguntado el 09 de enero de 11 a las 01:01

1 Respuestas

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.

Respondido el 09 de enero de 11 a las 04:01

Downvoting because this does not answer the original question, the question was how to specify expected value. And you just explained why example code doesn't work. - Vitaliy Lebedev

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.