[rspec-users] How do BOTH these examples pass?

Ashley Moran ashley.moran at patchspace.co.uk
Tue Sep 30 10:51:33 EDT 2008


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/
http://aviewfromafar.net/





More information about the rspec-users mailing list