How do BOTH these examples pass?

Hi

I’m, being thick, right?:

describe RssReader do
before(:each) do
@uri = mock(URI)
::RSS::Parser.stub!(:parse).and_return(:parsed_rss)
@rss_reader = RssReader.new(@uri)
end

describe "when the RSS content loads" do
  # ...
end

describe "when there is an HTTP error" do
  before(:each) do
    # second argument to OpenURI::HTTPError.new is an IO
    @uri.stub!(:read).and_raise(OpenURI::HTTPError.new("Fake

HTTPError", nil))
end

  # ...

  it "should not attempt to parse the RSS" do
    ::RSS::Parser.should_receive(:parse)
    @rss_reader.rss
  end

  it "should not attempt to parse the RSS" do
    ::RSS::Parser.should_not_receive(:parse)
    @rss_reader.rss
  end
end

end

Here’s the output:

% spec -c -fs rss_reader_spec.rb
RssReader when the RSS content loads

  • should parse the RSS
  • should give access to the items

RssReader when there is an HTTP error

  • should attempt to parse the RSS
  • should not attempt to parse the RSS
  • should fail gracefully

Finished in 0.030693 seconds

5 examples, 0 failures

And the code (not the final state I want it in, mind):

class RssReader
  def initialize(uri)
    @uri = uri
  end

  def rss
    rss_body =
      begin
        @uri.read
      rescue OpenURI::HTTPError => e
        nil
      end

    ::RSS::Parser.parse(rss_body, false)
  end
end

Any ideas anyone?

Thanks
Ashley


http://www.patchspace.co.uk/

On 30 Sep 2008, at 15:51, Ashley M. wrote:

it “should not attempt to parse the RSS” do

Or,
it “should attempt to parse the RSS” do

Please excuse the typo…

Ashley


http://www.patchspace.co.uk/

On Wed, Oct 1, 2008 at 1:02 AM, Ashley M.
[email protected] wrote:

RssReader when there is an HTTP error

  • should attempt to parse the RSS
  • should not attempt to parse the RSS
  • should fail gracefully

From what I can see, your code error is not doing anything with the
caught exception:

def rss
  rss_body =
    begin
      @uri.read
    rescue OpenURI::HTTPError => e
      nil
    end

  ::RSS::Parser.parse(rss_body, false)
end

This starts reading the body, catches an error, evaluates nil, exits
the rescue block and then continues to do the Parser.parse.

Changing it to:

def rss
  rss_body =
    begin
      @uri.read
      ::RSS::Parser.parse(rss_body, false)
    rescue OpenURI::HTTPError => e
      nil
    end
end

Should pass your specs.

Mikel

On Wed, Oct 1, 2008 at 11:26 PM, Ashley M.
[email protected] wrote:

does pass the specs, both the one that says parse should be called, and
the one that say it shouldn’t! But why? I’m prepared to admit I’m just
missing something really really really obvious (it happens often), but how
can:

both pass, under any circumstances?

Oh… I see what you are saying now…

Weird.

When I have had something like this in the past, I refactor my spec
code to not use any nesting at all and put all the pre conditions
inside of the it should block, that way you are sure you are not
getting some weird edge case on something.

Try it out.

it “should not pass” do
every setup thing from every before block in here
do the action
end

See what comes of that.

Ashley M. [email protected] writes:

it "should not attempt to parse the RSS" do
  ::RSS::Parser.should_not_receive(:parse)
  @rss_reader.rss
end

both pass, under any circumstances?

I think this may be a bug that I introduced recently. Fortunately I
also think someone already wrote a patch :slight_smile: Check out
http://rspec.lighthouseapp.com/projects/5645/tickets/548 and see if it
solves your problem.

Pat

On 1 Oct 2008, at 14:11, Mikel L. wrote:

end
Ah don’t worry, that’s what my real code does. The code I posted was
mid-refactoring.

Should pass your specs.

It’s academic really, but this code:

def rss
rss_body =
begin
@uri.read
rescue OpenURI::HTTPError => e
nil
end

 ::RSS::Parser.parse(rss_body, false)

end

does pass the specs, both the one that says parse should be called,
and the one that say it shouldn’t! But why? I’m prepared to admit
I’m just missing something really really really obvious (it happens
often), but how can:

 it "should attempt to parse the RSS" do
   ::RSS::Parser.should_receive(:parse)
   @rss_reader.rss
 end

 it "should not attempt to parse the RSS" do
   ::RSS::Parser.should_not_receive(:parse)
   @rss_reader.rss
 end

both pass, under any circumstances?

Ashley


http://www.patchspace.co.uk/

On 1 Oct 2008, at 15:23, Pat M. wrote:

I think this may be a bug that I introduced recently. Fortunately I
also think someone already wrote a patch :slight_smile: Check out
http://rspec.lighthouseapp.com/projects/5645/tickets/548 and see if it
solves your problem.

Aha! That’s it.

That’s a pretty bad bug though! Will there be an RSpec 1.1.6 soon to
patch it?

Thanks
Ashley


http://www.patchspace.co.uk/

Ashley M. [email protected] writes:

patch it?
I don’t know, we’ll late David determine how critical it is and whether
it warrants a new release.

fwiw, I’ve run that code against several rails apps and open source
projects with no problems. So while I agree that it’s a nasty little
bug, I’m not sure how often it comes up in practice? Maybe we can knock
out a bunch of outstanding bug reports in the next couple days and push
out 1.1.6.

Pat

On Wed, Oct 1, 2008 at 10:32 AM, Pat M. [email protected] wrote:

out 1.1.6.
There were a couple of other bugs introduced in 1.1.5 so I would like
to get out a 1.1.6 very soon. Probably over the weekend if we can get
them all fixed.

Cheers,
David

On Oct 01, 2008, at 6:39 pm, David C. wrote:

There were a couple of other bugs introduced in 1.1.5 so I would like
to get out a 1.1.6 very soon. Probably over the weekend if we can get
them all fixed.

Cool thanks David.

It’s not such an issue now I know how to work round it but it was a
nasty gotcha.

Cheers
Ashley


http://www.patchspace.co.uk/

Ashley M. [email protected] writes:

nasty gotcha.
btw this is applied in trunk, so if you haven’t applied the patch
yourself yet then you can just update to the latest code and you’re set.

Pat

On Oct 02, 2008, at 2:53 am, Pat M. wrote:

btw this is applied in trunk, so if you haven’t applied the patch
yourself yet then you can just update to the latest code and you’re
set.

Cool thanks for the info Pat

Ashley


http://www.patchspace.co.uk/