[rspec-users] why should_receive failure?

David Chelimsky dchelimsky at gmail.com
Tue Jul 14 23:37:20 EDT 2009

On Tue, Jul 14, 2009 at 9:59 PM, Zhenning Guan<lists at ruby-forum.com> wrote:
> class PeepCode
>  def awesome
>    "awesome"
>  end
> end
> describe PeepCode do
>  it "should fuck" do
>   PeepCode.new.should_receive(:awesome).and_return("awesome")
>  end
> end
> -----------------------------------------
> Spec::Mocks::MockExpectationError in 'PeepCode should fuck'
> #<PeepCode:0xb7aa3bbc> expected :awesome with (any args) once, but
> received it 0 times
> ./simple_spec.rb:13:
> Finished in 0.006159 seconds
> 1 example, 1 failure
> ------------------------------------------
> what's wrong with my code?

You asked a similar question yesterday and I tried to explain. Did you
not receive my response?

should_receive sets an expectation that a subsequent event will cause
the PeepCode object to receive the :awesome message. There is no code
after that expectation is set, so the message is never received, and
the expectation fails. That's exactly what the error message is
telling you: <PeepCode:0xb7aa3bbc> expected :awesome with (any args)
once, but received it 0 times.

If you're trying to specify that the PeepCode object should return
"awesome" when you call the awesome() method, then the example should
look like this:

describe PeepCode, "#awesome" do
  it "should return 'awesome'" do
    peepcode = PeepCode.new
    peepcode.awesome.should == "awesome"

There's no need to set a message expectation (e.g. mock) here.


ps - I think it's OK to say "fuck" on this list, if that's what you
really mean, but I don't really understand what you're getting at with
the expectation that PeepCode should fuck. Perhaps you might consider
being a bit more careful about the examples you send.

More information about the rspec-users mailing list