2012-03-28 5 views
0

私は、次の方法があります。例外これらの異なるタイプが提起されていることをテストする方法はmessage expectationsを使用することであるようにRSpecの私の限られた理解に基づいてそのメソッドをテストする方法は、さまざまな種類の例外を発生させる必要がありますか?

def call_http_service(url, url_params) 
    begin 
     conn = create_connection(url) 
     resp = get_response(conn, url_params) 
     raise_if_http_status_error(resp) 
     xml_resp = parse_xml(resp) 
     raise_if_client_status_error(xml_resp) 

     return xml_resp 
    rescue ClientError => e 
     raise ClientError, "Error interacting with feed at #{url}: #{e.message}" 
    rescue Faraday::Error::ClientError => e 
     raise ClientError, "Error interacting with feed at #{url}: #{e.message}" 
    rescue Nokogiri::XML::SyntaxError => e 
     raise ClientParseError, "Error parsing response from #{url}: #{e.message}" 
    rescue => e 
     raise e 
    end 
    end 

を、それが見えます。あなたはそれにどのようにアプローチするのですか?

答えて

2

それはこのようなものになります。

it "raises ClientError when the HTTP request raises ClientError" 
    # stub the http request here to raise the error 
    expect do 
    subject.call_http_service 'http://example.com' 
    end.to raise_error(ClientError) 
end 

注:別のエラーを救出し、再呼出しはコードのにおいです。

関連する問題