[rspec-users] why should_receive failure?

Jim Gay jim at saturnflyer.com
Tue Jul 14 23:36:54 EDT 2009


On Jul 14, 2009, at 10:59 PM, Zhenning Guan wrote:

>
> class PeepCode
>
> def awesome
>   "awesome"
> end
>
> end
>
> describe PeepCode do
>
> it "should fuck" do
peep = PeepCode.new
peep.should_receive(:awesome).and_return("awesome")
peep.awesome #this completes the expectation above
> #  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?

I think you misunderstand "should_receive". It doesn't actually call  
the method you're describing; it creates an expectation. So your spec  
says that something should happen, and then you never called the  
method, so it "received it 0 times"

This is a bad example though.

Your code seems to say that this is the spec:

describe PeepCode, "awesome" do
  it "should return the string 'awesome'" do
    PeepCode.new.awesome.should == 'awesome'
  end
end

"should_receive" says that in the test which is being run, the object  
should receive that method call... for example:

class PeepCode
  def awesome
    totally_rad
  end
  def totally_rad
    "righteous"
  end
end

and your spec:

describe PeepCode, "awesome" do
  it "should call the totally_rad method" do
    peep = PeepCode.new
    peep.should_receive(:totally_rad).and_return(...whatever...)
    peep.awesome
  end
  it "should return the string 'righteous'" do
    PeepCode.new.awesome.should == 'righteous'
  end
end

I haven't tried any of that, but it should work.


Jim Gay
http://www.saturnflyer.com


More information about the rspec-users mailing list