[rspec-users] why should_receive doesn't work, but stub does.

David Chelimsky dchelimsky at gmail.com
Tue Jul 14 06:35:44 EDT 2009


On Tue, Jul 14, 2009 at 4:49 AM, Zhenning Guan<lists at ruby-forum.com> wrote:
>  before(:each) do
>     @myinstance = GetHtml.new
>  end
>
>  it "should be a Hyricot" do
>    hy = open('test.html') {|f| Hpricot f}
>    @myinstance.should_receive(:find_html).with("file:///test_url").and_return(hy)
>
>  end
> ++++++++++++++++++++++++++++++++++
>  it "should be a Hyricot" do
>    hy = open('test.html') {|f| Hpricot f}
>    @myinstance.stub!(:find_html).with("file:///test_url").and_return(hy)
>
>  end
> ++++++++++++++++++++++++++++++++++
>
> expected ................ once, but received it 0 times
>
> I have a find_html method in my GetHtml class, but why can't
> should_receive run successfuly? but use stub!, it can.

Both stub! and should_receive are intended to run *before* the event
in an example. In both examples, nothing happens after they are run.
The reason that the stub! appears to be running successfully is that
there are no expectations being set anywhere. In essence, that example
doesn't actually test anything

The should_receive example expects a call to find_html *after* the
expectation is set. That never happens, so it fails.

In neither case is there any interaction with the instance of GetHtml
other than to set the stub/expectation on it. What is it that you're
trying to specify?


More information about the rspec-users mailing list