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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Asd" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Asd
                    <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">
								<h1><a name="p-370093-repatchhttpsforumelixirforumcomtrepatch-everything-you-need-for-mocking-and-patching66937-160-1" class="anchor" href="#p-370093-repatchhttpsforumelixirforumcomtrepatch-everything-you-need-for-mocking-and-patching66937-160-1" aria-label="Heading link" rel="nofollow"></a><a href="https://forum.elixirforum.com/t/repatch-everything-you-need-for-mocking-and-patching/66937" rel="nofollow">Repatch</a> 1.6.0</h1>
<h2><a name="p-370093-coverage-2" class="anchor" href="#p-370093-coverage-2" aria-label="Heading link" rel="nofollow"></a>Coverage</h2>
<p>This update adds first-class coverage support for patched modules. It utilized new OTP 27 native coverage and supports non-native coverage for OTP versions prior to OTP 26. Coverage support is introduced in compatible way <strong>without <code>:cover</code> module private functions hacking like in mex and mimic libraries</strong>.</p>
<p>Feel free to try it out in your project. <a href="https://hexdocs.pm/repatch/Repatch.CoverTool.html" rel="noopener nofollow ugc">doc</a>.</p>
<h2><a name="p-370093-repatchnotify-3" class="anchor" href="#p-370093-repatchnotify-3" aria-label="Heading link" rel="nofollow"></a>Repatch.notify</h2>
<p>A function which sends a message to the calling process the specified function is called. Current implementation uses Repatch patching, but future implementations will use <code>:trace</code> and <code>:erlang.trace</code> which are a little bit more efficient.</p>
<h2><a name="p-370093-repatchhistory-4" class="anchor" href="#p-370093-repatchhistory-4" aria-label="Heading link" rel="nofollow"></a>Repatch.history</h2>
<p>The name speaks for itself, since this function just allows user to access the full history (or some filtered part of it) of all calls to the patched modules.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="370093" 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/repatch-everything-you-need-for-mocking-and-patching/66937/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-370093" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="370093"
                     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="370155" data-post-id="370155">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Interesting approach with the recompiler, this makes the library very powerful.</p>
<p>In <a href="https://forum.elixirforum.com/t/efx-a-library-to-declaratively-write-testable-effects/64574" class="inline-onebox" rel="nofollow">Efx - A library to declaratively write testable effects</a> we used process dictionaries to achieve async testing. What mechanism do you use?</p>
<p>You may want to add this library to your comparison as it is very very similar. It is more lightweight but we need to be explicit in code, about what we want to be bindable.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="370155" 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/repatch-everything-you-need-for-mocking-and-patching/66937/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-370155" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="370155"
                     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="370575" data-post-id="370575">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Asd" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Asd
                    <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>Hi, thanks!</p>
<p>Repatch currently has 3 isolation modes</p>
<ul>
<li><code>local</code> mode which applies patch only to the single process. It uses process dictionary as the storage for patches and is the fastest one in terms of Repatch overhead</li>
<li><code>global</code> and <code>shared</code> mode which apply patch to all or some processes respectively. These modes use global <code>ets</code> tables as the storage for patches</li>
</ul> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="370575" 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/repatch-everything-you-need-for-mocking-and-patching/66937/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-370575" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="370575"
                     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="379911" data-post-id="379911">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you for this library!</p>
<p>I have a small issue where I use Repatch to test the today’s date inside a LiveView.</p>
<p>It works well if I use <code>mode: :shared</code> for each test, but I wanted to make it <code>:shared</code> for the whole LiveView test. So I thought I could do <code>use Repatch.ExUnit, isolate_env: :shared</code> as written in the <a href="https://hexdocs.pm/repatch/Repatch.ExUnit.html#module-options" rel="noopener nofollow ugc">documentation</a>, but this does not seem work.</p>
<p>I am not sure if I am doing something wrong or if it does not work with <a href="https://hexdocs.pm/repatch/Repatch.Expectations.html#content" rel="noopener nofollow ugc">expectations</a> or even it is actually normal.</p>
<p>I have published a repository to repository to reproduce this case <a rel="nofollow">here</a>. The line were Repatch is used is <a href="https://github.com/aifrak/repatch-isolate-env-shared/blob/140737fe7c81817a615d4143f5c69cfd9867ba81/test/hello_world_web/live/product_live_test.exs#L30" rel="noopener nofollow ugc">here</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="379911" 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/repatch-everything-you-need-for-mocking-and-patching/66937/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-379911" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379911"
                     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="379937" data-post-id="379937">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Asd" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Asd
                    <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>Hi, thanks for the feedback, I appreciate it</p>
<ol>
<li><code>isolate_env</code> option is an option to isolate application env. That’s to limit the scope of <code>Application.get_env</code> and <code>Application.put_env</code> functions. See <a href="https://hexdocs.pm/repatch/Repatch.Application.html" class="inline-onebox" rel="noopener nofollow ugc">Repatch.Application — Repatch v1.6.1</a></li>
<li>You should pass a <code>mode</code> option to the <code>expect</code> call, like<pre data-code-wrap="elixir"><code class="lang-elixir">expect(Date, :utc_today, [mode: :shared, exactly: 2], fn -&gt; ~D[2023-04-06] end)
</code></pre>
</li>
</ol>
<p>So</p>
<ul>
<li>
<p>I will improve the documentation about this <code>isolate_env</code> option.</p>
</li>
<li>
<p>And since I can see that you wanted to pass the default mode option on the <code>use Repatch.ExUnit</code> I think I will consider something like <code>default_mode</code> option, but I my biggest argument against this is that it is less explicit and reader of the test will be confused if this patch is local, shared or global unless they check one of the many line in the head of the module</p>
<p>Perhaps I can implement some <code>setup</code> in the <code>use Repatch.ExUnit</code> to take the mode from test tag, like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@tag repatch_mode: :shared
test "..." do
  Repatch.patch(DateTime, :utc_now, fn -&gt; :ok end) # &lt;- this patch is shared mode now
end
</code></pre>
</li>
</ul>
<p>Share your ideas if you have any</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="379937" 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/repatch-everything-you-need-for-mocking-and-patching/66937/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-379937" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379937"
                     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="379953" data-post-id="379953">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks for your answer!</p>
<blockquote>
<p>I will improve the documentation about this <code>isolate_env</code> option.</p>
</blockquote>
<p>Thank you for that!</p>
<blockquote>
<p>Perhaps I can implement some <code>setup</code> in the <code>use Repatch.ExUnit</code> to take the mode from test tag, like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">@tag repatch_mode: :shared
test "..." do
  Repatch.patch(DateTime, :utc_now, fn -&gt; :ok end) # &lt;- this patch is shared mode now
end
</code></pre>
</blockquote>
<p>I think it is fine to keep all options together like you previously wrote:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">expect(Date, :utc_today, [mode: :shared, exactly: 2], fn -&gt; ~D[2023-04-06] end)
</code></pre>
<p>It is indeed more explicit as you said. I just wish that we could add <code>@tag</code> at the top of <code>describe</code> but I don’t think it is possible to do that.</p>
<p>I was just searching a way to avoid adding <code>mode: :shared</code> to all <code>expect</code> inside a LiveView test, because everything inside a LiveView is in the LiveView process or in its child processes. As result, all tests for LiveView will have all have `mode: :shared`.</p>
<p>The trick that I have done so far to avoid repetition of the options was something like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@repatch_options [mode: shared, at_least: :once]
</code></pre>
<p>Then inside the <code>test</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">expect(Date, :utc_today, @repatch_options, fn -&gt; ~D[2023-04-06] end
</code></pre>
<p>But maybe this is an anti-pattern as it is then less explicit.</p>
<p>I will follow your way and make it more explicit when I use <code>expect</code> by adding the options <code>[mode: :shared, …]</code> to all my tests.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="379953" 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/repatch-everything-you-need-for-mocking-and-patching/66937/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-379953" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379953"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="390105" data-post-id="390105">
  <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>If I define a mock for a protocol, should I allow for it to be used in different processes or it’s the same as in Promox - the struct carries some reference and no allowances are needed?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="390105" 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/repatch-everything-you-need-for-mocking-and-patching/66937/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-390105" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="390105"
                     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="390113" data-post-id="390113">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Asd" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Asd
                    <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>Mock for a protocol is just a module with <code>defstruct</code> plus the module which implmenets the protocol for this mock.</p>
<p>Consider this example</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Mix.install [:repatch]

Repatch.setup()

defprotocol Proto do
  def f(x, y)
end

Repatch.Mock.define(My, protocol: Proto)
# It is essentially just the `defmodule` with `defstruct` and `defimpl Proto` inside

my = struct(My, [])

IO.inspect Proto.f(my, 123)
# raises
# ** (Repatch.Mock.NotImplemented) Not implemented
#     Proto.My."REPATCH-f"/2
#     file.ex:11: (file)

Repatch.patch(Proto.My, :f, fn _x, y -&gt; y * 2 end)
# Note that we patch Proto.My module here. It is the module
# which contains specific implementation and it is automatically
# generated and compiled for every `defimpl` (which was done
# during the `Repatch.Mock.define`)
# It is `local` by deafult. If you want, you can override the scope
# and use allowances just like with any other patch

IO.inspect Proto.f(my, 123)
# prints 246
</code></pre>
<p>Repatch does not store the reference to the patch state in the structure, it is intentional.</p>
<hr>
<p>If you could describe your initial problem, I may be able to come up with the way I’d solve it with Repatch</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="390113" 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/repatch-everything-you-need-for-mocking-and-patching/66937/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-390113" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="390113"
                     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>