<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bhushan Kalode on Medium]]></title>
        <description><![CDATA[Stories by Bhushan Kalode on Medium]]></description>
        <link>https://medium.com/@bhushankalode12?source=rss-11f30b42986a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1oSxxX5fezmHuGHV.jpg</url>
            <title>Stories by Bhushan Kalode on Medium</title>
            <link>https://medium.com/@bhushankalode12?source=rss-11f30b42986a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 02:14:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bhushankalode12/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mocking Third Party API’s]]></title>
            <link>https://bhushankalode12.medium.com/mocking-third-party-apis-e8ce1d810b75?source=rss-11f30b42986a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8ce1d810b75</guid>
            <category><![CDATA[rspec]]></category>
            <category><![CDATA[webmock]]></category>
            <dc:creator><![CDATA[Bhushan Kalode]]></dc:creator>
            <pubDate>Mon, 05 Apr 2021 05:45:27 GMT</pubDate>
            <atom:updated>2021-04-05T05:45:27.052Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*KM6A7P-FqE6sFDzK" /></figure><p>Everyone uses third party APIs in their project. These are the external services which allows you to access third party functionalities within the project without rebuilding them.</p><p>While writing test cases for any project it is very difficult to be sure that the test suites always run successfully as it is dependent on the third party API response. Hence, for handling such scenarios one has to mock third party APIs to be sure that our test cases does not hit any actual third party API service and that it runs in isolation.</p><p>Mocking third party API is just recording the response of the third party API call.</p><h3>REASONS TO MOCK THIRD PARTY API.</h3><p>One should prefer to mock third party APIs in order to avoid failure of test case arising due to issues at third party API end. The reasons for same may include:</p><ul><li>Connectivity issues.</li><li>Server issues</li><li>Timeout response</li><li>Authentication Issue.</li></ul><h3>IMPLEMENTATION</h3><p>Let’s add rspec-rails gem to the Gemfile under the applicable group.</p><pre>group :development, :test do<br>  gem &#39;rspec-rails&#39;, &#39;~&gt; 4.1.0&#39;<br>end</pre><p>Let’s write one test case which will run successfully assuming a good connectivity and a successful response from <a href="https://www.facebook.com/&#39;"><strong>https://www.facebook.com/</strong></a>.</p><pre>require &quot;spec_helper&#39;<br>require &quot;net/http&quot;<br>RSpec.describe &quot;Stubbing external services&quot; do<br>  it &quot;Should get response from facebook&quot; do<br>    uri = URI(&quot;<a href="https://www.facebook.com/">https://www.facebook.com/</a>&quot;)<br>    response = Net::HTTP.get(uri)<br>    expect(response).to be_an_instance_of(String)<br>  end<br>end</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/0*SVaTvq_wrvJTKGhP.png" /></figure><p>In this case , actual response received from third party API will be compared.But if there is any server or connectivity issue while running the test cases , one will receive below mentioned error message.</p><pre>F<br>Failures:<br>1) Stubbing external services Should get response from facebook<br>     Failure/Error: response = Net::HTTP.get(uri)<br>      <br>     SocketError:<br>       Failed to open TCP connection to <a href="http://www.facebook.com:443/">www.facebook.com:443</a> (getaddrinfo: Temporary failure in name resolution)<br>     # ./spec/stub_request_spec.rb:22:in `block (2 levels) in &lt;top (required)&gt;&#39;<br>     # ------------------<br>     # --- Caused by: ---<br>     # SocketError:<br>     #   getaddrinfo: Temporary failure in name resolution<br>     #   ./spec/stub_request_spec.rb:22:in `block (2 levels) in &lt;top (required)&gt;&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/255/0*NYiGLIa1kO5lXBh0.jpeg" /></figure><p>Prominent solution to resolve this issue is to <strong>stub(mock)</strong> the used third party API request in order to avoid direct dependency on them.</p><p>Introducing <strong>webmock</strong> gem to stub the third party external services.</p><p>Let’s start by adding <strong>webmock</strong> gem to the gemfile under applicable group.</p><pre>group :test do<br>  # Adds support for Capybara system testing and selenium driver<br>  gem &#39;capybara&#39;, &#39;&gt;= 2.15&#39;<br>  gem &#39;selenium-webdriver&#39;<br>  # Easy installation and use of web drivers to run system tests with browsers<br>  gem &#39;webdrivers&#39;<br><strong>  gem &#39;webmock&#39;</strong><br>end</pre><h3>BLOCK THIRD PARTY API CALLING.</h3><p>To block calling third party API , we need to add some configurations, let’s disable external request by configuring the spec_helper.rb</p><pre>require &#39;webmock/rspec&#39;<br>RSpec.configure do |config|<br>  WebMock.disable_net_connect!(allow_localhost: true)<br>  # rest of predefined file content<br>end</pre><p>Now let’s run the test cases again and see what happens!</p><pre>rspec spec/stub_request_spec.rb <br>F<br>Failures:<br>1) Stubbing external services Should get response from facebook<br>     Failure/Error: response = Net::HTTP.get(uri)<br>      <br>     WebMock::NetConnectNotAllowedError:<br>       Real HTTP connections are disabled. Unregistered request: GET <a href="https://www.facebook.com/">https://www.facebook.com/</a> with headers {&#39;Accept&#39;=&gt;&#39;*/*&#39;, &#39;Accept-Encoding&#39;=&gt;&#39;gzip;q=1.0,deflate;q=0.6,identity;q=0.3&#39;, &#39;Host&#39;=&gt;&#39;<a href="http://www.facebook.com&/#39;">www.facebook.com&#39;</a>, &#39;User-Agent&#39;=&gt;&#39;Ruby&#39;}<br>      <br>       You can stub this request with the following snippet:<br>      <br>       stub_request(:get, &quot;<a href="https://www.facebook.com/">https://www.facebook.com/</a>&quot;).<br>         with(<br>           headers: {<br>          &#39;Accept&#39;=&gt;&#39;*/*&#39;,<br>          &#39;Accept-Encoding&#39;=&gt;&#39;gzip;q=1.0,deflate;q=0.6,identity;q=0.3&#39;,<br>          &#39;Host&#39;=&gt;&#39;<a href="http://www.facebook.com&/#39;">www.facebook.com&#39;</a>,<br>          &#39;User-Agent&#39;=&gt;&#39;Ruby&#39;<br>           }).<br>         to_return(status: 200, body: &quot;&quot;, headers: {})<br>      <br>       ============================================================<br>Finished in 0.00375 seconds (files took 0.2682 seconds to load)<br>1 example, 1 failure</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/288/0*DF0jbIzQ1ncFZxjD.jpeg" /></figure><p>Let’s stub the request and see the output again.</p><pre>require &quot;spec_helper&quot;<br>require &quot;net/http&quot;<br>RSpec.describe &#39;Stubbing external services&#39; do<br>  <br>  before(:each) do<br>    stub_request(:get, &quot;<a href="https://www.facebook.com/">https://www.facebook.com/</a>&quot;).<br>    with(<br>      headers: {<br>        &#39;Accept&#39;=&gt;&#39;*/*&#39;,<br>        &#39;Accept-Encoding&#39;=&gt;&#39;gzip;q=1.0,deflate;q=0.6,identity;q=0.3&#39;,<br>        &#39;Host&#39;=&gt;&#39;<a href="http://www.facebook.com&/#39;">www.facebook.com&#39;</a>,<br>        &#39;User-Agent&#39;=&gt;&#39;Ruby&#39;<br>      }).<br>    to_return(status: 200, body: &quot;success&quot;, headers: {})<br>  end<br>  <br>  it &quot;Should get response from facebook&quot; do<br>    uri = URI(&quot;<a href="https://www.facebook.com/">https://www.facebook.com/</a>&quot;)<br>    response = Net::HTTP.get(uri)<br>    expect(response).to be_an_instance_of(String)<br>  end<br>end</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*0w6mQupW7pWoITFd.png" /></figure><p><strong>Advantages</strong></p><p>The advantage of web mocking is that you can focus on implementing behaviour of the code instead of worrying about implementation of third party APIs.</p><p><strong>Disadvantages</strong></p><p>Its observed that mocking third party API’s can lack protection ,which means any changes in third party API response will not be compatible to catch it.</p><p>One possible solution to overcome this problem testing the code once a day manually by calling actual third party API’s and then rely on mock test suites thereafter.</p><p><strong>Conclusion</strong></p><p>I would recommend using <strong>webmock gem</strong> among others as it is easier to implement and understand. For more details refer <a href="https://ruby.libhunt.com/webmock-alternatives">here</a>.</p><p>Thank You!! Happy Coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8ce1d810b75" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>