<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="298699" data-post-id="298699">
  <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">
								<p>There’s also <a href="https://hexdocs.pm/mimic/Mimic.html" class="inline-onebox" rel="noopener nofollow ugc">Mimic — Mimic v2.3.0</a>. I haven’t tried it yet though.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="298699" 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/how-to-mock-functions-from-elixir-built-in-modules/57778/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-298699" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="298699"
                     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="298722" data-post-id="298722">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Generally, a mocked function comes with expectations which form a specification of how the function is going to be called. Stubs, on the other hand, only provide predefined answers to calls and, in my experience, doesn’t lead to testing calling logic which shouldn’t matter most of the times.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="298722" 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/how-to-mock-functions-from-elixir-built-in-modules/57778/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-298722" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="298722"
                     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="298880" data-post-id="298880">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Your <code>build/1</code> function is begging for an integration test because it “speaks to the outside world” (even though it’s the local file system.)</p>
<p>You could consider using fixture files, where one is valid and one is invalid, and you could assert accordingly.</p>
<p>Even though <a href="https://8thlight.com/insights/thats-not-yours" rel="noopener nofollow ugc">this post</a> is about Ruby, I like the “Don’t mock what you don’t own” philosophy and I’ve happily used it in Elixir. I’d argue that you don’t “own” <code>File</code>.</p>
<p>Another option would be to inject the <code>File.read/1</code> dependency, so your function could change as such:</p>
<pre data-code-wrap="diff"><code class="lang-diff">- def build(file_name) do
+ def build(file_name, opts \\ []) do
+ reader = opts[:reader] || &amp;File.read/1
  file_name
- |&gt; File.read()
+ |&gt; reader.()
  |&gt; process_file_result()
end
</code></pre>
<p>(I just hand-wrote that <code>diff</code> above, please excuse any errors)</p>
<p>Sometimes I use <code>DI</code> because it’s more convenient, but I think in this specific example I’d personally opt for the fixture files.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="298880" 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/how-to-mock-functions-from-elixir-built-in-modules/57778/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-298880" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="298880"
                     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="299041" data-post-id="299041">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kenny-evitt" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kenny-evitt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve found that it’s occasionally useful to move a private function that I want to test to an ‘internal module’, e.g. <code>FizzBuzz.FileProcessing</code>. That seems to be pretty clear on its own but you can, of course, also make it explicit with a suitable module-doc:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule FizzBuzz.FileProcessing
  @moduledoc """
  This is an internal module for `FizzBuzz`.
  """

  ...
</code></pre>
<p>I’ve also found that it’s often fine – good even – to just use a test file, i.e. “read from my filesystem”.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="299041" 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/how-to-mock-functions-from-elixir-built-in-modules/57778/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-299041" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="299041"
                     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="299044" data-post-id="299044">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="kenny-evitt" data-post="15" data-topic="57778">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/e47774/48.png" class="avatar"> kenny-evitt:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule FizzBuzz.FileProcessing
  @moduledoc """
  This is an internal module for `FizzBuzz`.
  """
</code></pre>
</blockquote>
</aside>
<p>By convention <code>@moduledoc false</code> modules are to be considered private.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="299044" 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/how-to-mock-functions-from-elixir-built-in-modules/57778/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-299044" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="299044"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>