[rspec-users] Specifying a specific error is re-raised

Ashley Moran ashley.moran at patchspace.co.uk
Tue Oct 7 10:50:33 EDT 2008


On 7 Oct 2008, at 15:33, David Chelimsky wrote:

> Regardless, if you really care about the specific instance you can  
> do this:
>
> lambda {
>  @connection.receive_data("UPDATE\n")
> }.should raise_error {|error| error.should equal(@error)}
>
> If what you care about is the message, you can expect specific
> messages using strings or regexps:

Thanks - that snippet does exactly what I want.  Basically I want tee  
pipe-fitting for errors - the code now looks like this:

     class Connection < EventMachine::Connection
       attr_accessor :server

       def receive_data(data)
         if data.chomp == "UPDATE"
           handle_update
         else
           send_data("ERROR: Unknown command\n")
         end
       end

       def unbind
         raise @error if @error
       end

       private

       def handle_update
         server.check_feeds
       rescue StandardError => @error
         send_data("ERROR: #{@error.to_s}\n")
         close_connection_after_writing
       else
         send_data("OK\n")
       end
     end

So I don't care about the exception beyond extracting enough data to  
feed back through the socket.

Cheers
Ashley

-- 
http://www.patchspace.co.uk/
http://aviewfromafar.net/





More information about the rspec-users mailing list