<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="242729" data-post-id="242729">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dimitarvp" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/120/38664_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dimitarvp
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="crispinb" data-post="11" data-topic="45992">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/crispinb/48/22642_2.png" class="avatar"> crispinb:</div>
<blockquote>
<p>With raw api returns available locally, everything’s always there to be mined later.</p>
</blockquote>
</aside>
<p>Yep. I’ve had 20MB JSON fixtures and just made utility functions to get pieces of them for smaller unit-testing purposes.</p>
<p>I went even further in one project: I made a special tag for certain tests (called it <code>:exhaustive</code>) and excluded it by default so we don’t overload our CI – but made it a policy to run the tests tagged with it once a week. They used the <em>complete</em> cached payloads from the live API, no matter how big they were (one was 215MB even; long live Zstandard level 19 compression and <code>git lfs</code>!).</p>
<p>A lot of teams grumble about this practice, for reasons they were never able to explain in a satisfactory manner to me, but I’ve uncovered a frightening amount of bugs in code <em>just by using cached real data</em>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242729" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-242729" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242729"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="242766" data-post-id="242766">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="neuone" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  neuone
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><strong>To Bypass or Mox</strong><br>
It can be overwhelming  trying to understand the difference between whether I should use a Mox or a Bypass. And at the same time trying to understand behaviours. I got the weekend to further educate and explore. Not complaining, just a lot of concepts at once and they have subtle differences.</p>
<p>I like the idea of using a Behaviour (a contract), that I then implement in a module that then I can then mock in my tests.</p>
<p>I have download real json results that I can reuse as your suggestions. I keep them in my tests.</p>
<p>This community is awesome, appreciate everyones feedback and direction so far.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242766" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-242766" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242766"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="242769" data-post-id="242769">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dimitarvp" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/120/38664_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dimitarvp
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="neuone" data-post="13" data-topic="45992">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/n/3da27b/48.png" class="avatar"> neuone:</div>
<blockquote>
<p>It can be overwhelming trying to understand the difference between</p>
</blockquote>
</aside>
<p>I sympathize, dude, but there simply are no shortcuts. At one point you have to roll up your sleeves and get your battle scars. Glad you are motivated to do it!</p>
<p>This community will be extremely helpful and supportive if you show that you’ve done your homework – or are willing to do it. So keep at it, you’ll be a master in no time!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242769" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-242769" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242769"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="242779" data-post-id="242779">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Matsa59" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Matsa59/120/20270_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Matsa59
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There is also <a href="https://hex.pm/packages/patch" rel="nofollow">patch</a></p>
<p>It’s not specifically related to http but it could allow you to override (patch) the function that make the api call <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p>Really nice lib btw</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242779" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-242779" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242779"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="242782" data-post-id="242782">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="trisolaran" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/trisolaran/120/23748_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  trisolaran
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="neuone" data-post="13" data-topic="45992">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/n/3da27b/48.png" class="avatar"> neuone:</div>
<blockquote>
<p><strong>To Bypass or Mox</strong><br>
It can be overwhelming trying to understand the difference between whether I should use a Mox or a Bypass.</p>
</blockquote>
</aside>
<p>This is how I decide. If the API module that talks to the external API (this would be <code>TwiterClient</code> in your example) is part of my codebase, I write tests for it and simulate the interactions with the external API using Bypass. If the module is part of a library, then it’s (hopefully) already tested and so I don’t write any tests for it. When testing a consumer of the API module, I mock the API module.</p>
<p>This seems pretty straightforward to me, but I’d be interested to hear what other people on this thread have to say, maybe they have a different opinion.</p>
<p>When writing tests using Bypass, I can confirm that <a class="mention" href="/u/dimitarvp" rel="nofollow">@dimitarvp</a>’s suggestions of dumping response from the real API to generate fixtures is indeed a very good one! I’m glad that others are also doing it <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242782" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-242782" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242782"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="242802" data-post-id="242802">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="crispinb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/crispinb/120/22642_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  crispinb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If you can keep this in the back of your mind to check out at some stage (ie. not to add to the immediate overwhelm!), bear in mind there’s at least one other approach to creating a module ‘api’, using <a href="https://elixir-lang.org/getting-started/protocols.html" rel="nofollow">protocols</a>. There’s a bit of back and forth about the pros and cons, and how they relate to mocking, here: <a href="https://forum.elixirforum.com/t/mox-and-protocols/15277/13" class="inline-onebox" rel="nofollow">Mox and Protocols? - #13 by svarlet</a>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242802" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-242802" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242802"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="242803" data-post-id="242803">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="crispinb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/crispinb/120/22642_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  crispinb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="trisolaran" data-post="16" data-topic="45992">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/trisolaran/48/23748_2.png" class="avatar"> trisolaran:</div>
<blockquote>
<p>This is how I decide. If the API module that talks to the external API</p>
</blockquote>
</aside>
<p>How do you define ‘talks to the API’ here? If for example your module uses HTTPoison (or similar), do you consider this all under your control, so use Bypass, or would you mock out HTTPoison (or perhaps your HTTPoison.Base callback module)?</p>
<p>Background: I find myself uncertain in concrete cases, similarly to <a class="mention" href="/u/neuone" rel="nofollow">@neuone</a>, re which approach to take (as I sometimes do with behaviours vs protocols). I tend to start by analogy with other languages I’ve used, but then the analogies don’t seem too convincing. I probably need to read more good Elixir code.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242803" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-242803" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242803"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="242804" data-post-id="242804">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="trisolaran" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/trisolaran/120/23748_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  trisolaran
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="crispinb" data-post="18" data-topic="45992">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/crispinb/48/22642_2.png" class="avatar"> crispinb:</div>
<blockquote>
<p>How do you define ‘talks to the API’ here? If for example your module uses HTTPoison (or similar), do you consider this all under your control, so use Bypass, or would you mock out HTTPoison (or perhaps your HTTPoison.Base callback module)?</p>
</blockquote>
</aside>
<p>If my module uses HTTPoison or Tesla, then I consider it to be the module talking to the API. HTTPoison and Tesla are convenience libraries that my module uses to send HTTP requests. However it’s my module that needs to decide which HTTP requests to send and therefore the one operating at the HTTP level and “talking to the API”.</p>
<p>As you mention, in this case, mocking HTTPoison or Tesla is an option. However, I don’t do that anymore and use Bypass instead. What convinced me is this article: <a href="https://medium.com/flatiron-labs/rolling-your-own-mock-server-for-testing-in-elixir-2cdb5ccdd1a0" rel="noopener nofollow ugc">https://medium.com/flatiron-labs/rolling-your-own-mock-server-for-testing-in-elixir-2cdb5ccdd1a0</a>, from the same author of the post you previously shared. I recommend reading it and also José’s article about mocks linked from it (<a href="http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/" class="inline-onebox" rel="noopener nofollow ugc">Mocks and explicit contracts « Plataformatec Blog</a>).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242804" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-242804" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242804"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="242807" data-post-id="242807">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="crispinb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/crispinb/120/22642_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  crispinb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks, very useful links. Echoing the old ‘how can I know what I think until I see what I write?’ cliche, I’m not entirely sure about the Bypass approach, not having used it yet. But I just happen to be working on an api client, currently using Mox, so I’ll give this approach a try and see how it looks.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242807" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-242807" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242807"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="242957" data-post-id="242957">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="crispinb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/crispinb/120/22642_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  crispinb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I did have one problem with this approach - which was getting my api module to use the Bypass endpoint. It’s buried a few dependency layers beneath my tests, and I didn’t want to pass params all the way through.</p>
<p>My first stab was to use Application config to allow a url override, which I set at runtime in tests. Then (duh!) I realised this messed up async tests. Not wanting to spend more time on this for now, I’ve turned off ExUnit async for those tests, but I consider that a workaround rather than a fix. I’ve found since there’s some discussion of this exact problem at <a href="https://forum.elixirforum.com/t/bypass-and-async-tests-with-ex-unit/45005" class="inline-onebox" rel="nofollow">Bypass and async tests with ex_unit</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242957" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-a-controller-that-makes-outside-api-calls/45992/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-242957" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242957"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/45992/load_more?page=3">Load more posts (9 remaining)</a>
</div></template></turbo-stream>