<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kaythinks</title>
    <description>The latest articles on DEV Community by kaythinks (@kaythinks).</description>
    <link>https://dev.to/kaythinks</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F129033%2F3d6decd4-2ca5-488e-93f6-83a6d0796ec3.jpeg</url>
      <title>DEV Community: kaythinks</title>
      <link>https://dev.to/kaythinks</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaythinks"/>
    <language>en</language>
    <item>
      <title>Mocking API calls in Laravel TDD</title>
      <dc:creator>kaythinks</dc:creator>
      <pubDate>Thu, 18 Jun 2020 16:44:28 +0000</pubDate>
      <link>https://dev.to/kaythinks/mocking-api-calls-in-laravel-tdd-12hi</link>
      <guid>https://dev.to/kaythinks/mocking-api-calls-in-laravel-tdd-12hi</guid>
      <description>&lt;p&gt;If you are used to TDD, you will definitely come across this at some point. Mocking an API client like Guzzle is a piece of cake in Laravel.&lt;br&gt;
As a test case, I want to mock a Guzzle call to get data from a certain endpoint.&lt;br&gt;
I will simply do the following &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Import the following classes into your test class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;use Mockery;&lt;/p&gt;

&lt;p&gt;use GuzzleHttp\Client;&lt;/p&gt;

&lt;p&gt;use GuzzleHttp\Psr7\Response; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a mockery object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;$mock = Mockery::mock(Client::class);&lt;/p&gt;

&lt;p&gt;NOTE:- the Client class is Guzzle Class.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a response variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;$response = json_encode(['data'=&amp;gt;'ok']);&lt;/p&gt;

&lt;p&gt;NOTE:- The array should contain the expected response you want.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now define what the mock object should do.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;$mock-&amp;gt;shouldReceive('get')-&amp;gt;once()-&amp;gt;andReturn(new Response(200,[],$response));&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finally, bind the mock object to an instance of the Guzzle client class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;$this-&amp;gt;app-&amp;gt;instance('GuzzleHttp\Client', $mock);&lt;/p&gt;

&lt;p&gt;After following these steps, anywhere you instantiate Guzzle class in your code will automatically call the mock object instead.&lt;/p&gt;

&lt;p&gt;Do you have other ideas about mocking API calls ?, feel free to comment.&lt;br&gt;
Ciao !&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
