<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="322639" data-post-id="322639">
  <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 quote-modified" data-username="benwilson512" data-post="11" data-topic="62603">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/48/1457_2.png" class="avatar"> benwilson512:</div>
<blockquote>
<p>That module both defines the behavior but also does …</p>
</blockquote>
</aside>
<p>This is what I eventually settled on as well but it feels very… unclean. Both a behaviour module and an implementation dispatcher. Two in one.</p>
<p>But I am the first guy around here to make a huge stink about having too many small files so yeah, I settled on that option and made myself hate it the least. <img src="https://forum.elixirforum.com/images/emoji/apple/grin.png?v=15" title=":grin:" class="emoji" alt=":grin:" 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="322639" 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/genserver-avoid-refresh-running-in-tests/62603/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-322639" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322639"
                     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="322677" data-post-id="322677">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I try to avoid introducing behaviour if this is only for test. If the behaviour already exists because the production code uses different implementations, then Mox all the way, but otherwise no. If the code calls an external thing such as an HTTP API we mock that with a real HTTP server. Even if it is controversial ; it works well in our case.</p>
<p>GenServers that do polling are just not started in test environment, and tested only in their own test. If the rest of the app must interact with it then we would rather have the GenServer only handle process stuff and delegate the logic to a functional module that will store the data somewhere, so we can call that directly in the tests to setup the data, and have the app interact with the data.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="322677" 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/genserver-avoid-refresh-running-in-tests/62603/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-322677" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322677"
                     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="322694" data-post-id="322694">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="belgoros" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/belgoros/120/39798_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  belgoros
                      <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>Wow, I provoked a huge wave of discussions, some of them are still beyond of my understanding. Thank you all for bringing your experience and knowledge stones to the base.<br>
What confuses me most is that n my simple app, I have a GenServer defies to be started as a child process wen the app starts:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># application.ex
@impl true
  def start(_type, _args) do
...
  {PaymentServer.Exchange.RatesMonitor, []}
]
end
</code></pre>
<p>where the module <code>RatesMonitor</code> is a GenServer which hits an external API server every ‘X’ minutes.</p>
<p>I tried to define a behaviour module as follows:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PaymentServer.Exchange.MonitorApi do
  @moduledoc """
  Handles Exchange server API calls
  """
  @callback get_rates(from_currency :: String.t(), to_currency :: String.t()) ::
              {:ok, any()} | {:error, any()}
end
</code></pre>
<p>Then in a real API module I declare this behavour:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PaymentServer.Exchange.AlphaVantageApi do
  @behaviour PaymentServer.Exchange.MonitorApi
  @moduledoc """
  This is a Alpha Vantage API module which provides the exchange rates data
  """
  @api_url "http://localhost:4001/query"

  @impl true
  def get_rates(from_currency \\ "USD", to_currency \\ "JPY") do
    url = build_search_url(from_currency, to_currency)

    case Req.get(url) do
      {:error, reason} -&gt; {:error, reason}
      {:ok, response} -&gt; response
    end
  end
...
end
</code></pre>
<p>Then in the GenServer module, I call it as follows:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PaymentServer.Exchange.RatesMonitor do
  @moduledoc """
    This is the implementation of an exchange rates monitor server
  """
  use GenServer
...
defp fetch_rates(%{from_currency: from_currency, to_currency: to_currency} = state) do
    api_module =
      Application.get_env(:payment_server, :api_module, PaymentServer.Exchange.AlphaVantageApi)

    response = api_module.get_rates(from_currency, to_currency)
    rate = Parser.parse(response)

    %{state | from_currency: from_currency, to_currency: to_currency, rate: rate}
  end
end
</code></pre>
<p>To be able to use a mock API module in tests, I created the following module:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PaymentServer.Exchange.MockAlphaVantageApi do
  @behaviour PaymentServer.Exchange.MonitorApi

  @impl true
  def get_rates(from_currency, to_currency) do
    """
    status: 200,
    body: %{
    "Realtime Currency Exchange Rate" =&gt; %{
      "1. From_Currency Code" =&gt; "EUR",
      "2. From_Currency Name" =&gt; "Euro",
      "3. To_Currency Code" =&gt; "USD",
      "4. To_Currency Name" =&gt; "US Dollar",
      "5. Exchange Rate" =&gt; "3.2045",
      "6. Last Refreshed" =&gt; "2024-03-31 16:47:21.920729Z",
      "7. Time Zone" =&gt; "UTC",
      "8. Bid Price" =&gt; "3.2045",
      "9. Ask Price" =&gt; "3.2045"
    }
    },
    """
  end
end
</code></pre>
<p>and put these lines in the <code>config/tests.exs</code> file:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">config :payment_server,
  api_module: PaymentServer.Exchange.MockAlphaVantageApi
</code></pre>
<p>A simple tests where I tried to check if the mock module would be used:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PaymentServer.Exchange.MockAlphaVantageApiTest do
  use ExUnit.Case, async: true

  test "it should call mocked API" do
    response = PaymentServer.Exchange.MonitorApi.get_rates("eur", "usd")
    IO.inspect response
  end
end
</code></pre>
<p>Before running the test, I stopped the API running in a Docker container and run <code>mix test test/payment_server/exchange/mock_alpha_vantage_api_test.exs</code> which failed with:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">** (Mix) Could not start application payment_server: PaymentServer.Application.start(:normal, []) returned an error: shutdown: failed to start child: PaymentServer.Exchange.RatesMonitor
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function PaymentServer.Exchange.MockAlphaVantageApi.get_rates/2 is undefined (module PaymentServer.Exchange.MockAlphaVantageApi is not available)
            PaymentServer.Exchange.MockAlphaVantageApi.get_rates(:usd, :eur)
            (payment_server 0.1.0) lib/payment_server/exchange/rates_monitor.ex:64: PaymentServer.Exchange.RatesMonitor.fetch_rates/1
            (payment_server 0.1.0) lib/payment_server/exchange/rates_monitor.ex:36: PaymentServer.Exchange.RatesMonitor.init/1
            (stdlib 4.3.1.2) gen_server.erl:851: :gen_server.init_it/2
            (stdlib 4.3.1.2) gen_server.erl:814: :gen_server.init_it/6
            (stdlib 4.3.1.2) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
</code></pre>
<p>So the question is why, despite all the above settings, the app could not start the GenServer module, <code>PaymentServer.Exchange.RatesMonitor</code> ?<br>
It bothers me why to run tests, I still need to have API instance running in a Docker container? Even when using the above <a href="https://hex.pm/packages/mimic" rel="nofollow">Mimic</a> lib, (which mock fine the requests), it is still 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="322694" 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/genserver-avoid-refresh-running-in-tests/62603/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-322694" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322694"
                     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="322697" data-post-id="322697">
  <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="lud" data-post="13" data-topic="62603">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/48/14382_2.png" class="avatar"> lud:</div>
<blockquote>
<p>I try to avoid introducing behaviour if this is only for test. If the behaviour already exists because the production code uses different implementations, then Mox all the way, but otherwise no. If the code calls an external thing such as an HTTP API we mock that with a real HTTP server. Even if it is controversial ; it works well in our case.</p>
</blockquote>
</aside>
<p>On a technical level this is indeed different, but on a functional level you have the interface in the form of an http api specification. Instead of switching out a module you’ll be switching out an endpoint. The only downside I see to this is that while you have an interface you’re not in control over the interface. The provider of the http api changing requirements breaks not just the integration with the actual API, but also all the rest of the codebase depending on the interface. If you wrap things in your own abstraction you can limit that blast radius.</p>
<p>This might also be a small push to define the interface by what the application needs rather than what the (current) provider provides.</p>
<aside class="quote no-group" data-username="belgoros" data-post="14" data-topic="62603">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/belgoros/48/39798_2.png" class="avatar"> belgoros:</div>
<blockquote>
<p>So the question is why, despite all the above settings, the app could not start the GenServer module, <code>PaymentServer.Exchange.RatesMonitor</code> ?</p>
</blockquote>
</aside>
<p><code>module PaymentServer.Exchange.MockAlphaVantageApi is not available</code>. This part of the error message is the key. It’s trying to call the correct module, but it’s not available. So the question would be where you put the module definition.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="322697" 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/genserver-avoid-refresh-running-in-tests/62603/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-322697" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322697"
                     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="322705" data-post-id="322705">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="belgoros" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/belgoros/120/39798_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  belgoros
                      <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><code>PaymentServer.Exchange.MockAlphaVantageApi</code> module is defined in <code>test/payment_server/exchange/mock_alpha_vantage_api.ex</code>. Should it be absolutely somewhere in the <code>lib</code> directory?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="322705" 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/genserver-avoid-refresh-running-in-tests/62603/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-322705" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322705"
                     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="322707" data-post-id="322707">
  <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">
								<p>By default only files in <code>lib/</code> are compiled. Test files are scripts and called by the test runner. However in mix.exs you can add additional folders for source files to be compiled. Many elixir projects configure <code>test/support</code> to be such a folder for the test mix env: <a href="https://til.hashrocket.com/posts/3y6morjjs7-add-elixir-files-to-your-compiled-list" class="inline-onebox" rel="nofollow">Add Elixir files to your compiled list - Today I Learned</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="322707" 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/genserver-avoid-refresh-running-in-tests/62603/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-322707" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322707"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="322708" data-post-id="322708">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="belgoros" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/belgoros/120/39798_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  belgoros
                      <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>Ah, I got it, thanks a lot!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="322708" 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/genserver-avoid-refresh-running-in-tests/62603/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-322708" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322708"
                     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="322720" data-post-id="322720">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>On a technical level this is indeed different, but on a functional level you have the interface in the form of an http api specification. Instead of switching out a module you’ll be switching out an endpoint.</p>
</blockquote>
<p>Yes but this interface has a single implementation in production code and so we test that only.</p>
<blockquote>
<p>The only downside I see to this is that while you have an interface you’re not in control over the interface. The provider of the http api changing requirements breaks not just the integration with the actual API, but also all the rest of the codebase depending on the interface.</p>
</blockquote>
<p>Indeed, if the HTTP API we depend on changes, we are screwed and have to chage the code. I don’t see how adding more layers to the story changes that infortunately. However I understand that you may get more time to change with more layers.</p>
<blockquote>
<p>If you wrap things in your own abstraction you can limit that blast radius. This might also be a small push to define the interface by what the application needs rather than what the (current) provider provides.</p>
</blockquote>
<p>Our production code that we test with a real http endpoint as a mock <em>is</em> our own abstraction, and provides what we need from that external dependency. If you add one more layer you still need to implement and test what to do when you get 400, 500, 403 errors etc.</p>
<p>What you get when splitting that in two parts is test speed, I concede, at the cost of more complexity.</p>
<p>Now if the endpoint returns <code>{"hello": "world"}</code> and our client returns <code>%Greeting{who: "world"}</code> then indeed we cannot just directly tell in the tests that the client will return that second form. But we can just pass that second form to the code that should handle it and test that it does. We just need one more test to be sure that there is “something” that will call the client and then pass that second form to the other part of the code.</p>
<p>In our app it works well like that because the “handler” part is dynamic and we can mock that with a simple module that will ensure that it receives the good data from a defined endpoint returned body. It depends on how things are layed out and there are so many possible ways to do things. Bottom line is I’d rather not add behaviours for the sake of 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="322720" 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/genserver-avoid-refresh-running-in-tests/62603/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-322720" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322720"
                     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="322758" data-post-id="322758">
  <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="LostKobrakai" data-post="15" data-topic="62603">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/48/3072_2.png" class="avatar"> LostKobrakai:</div>
<blockquote>
<p>On a technical level this is indeed different, but on a functional level you have the interface in the form of an http api specification.</p>
</blockquote>
</aside>
<p>The thing that actually sold me on passing an implementation as a function argument is the fact that in tests I can have it both ways: both <code>async: true</code> and clean-ish production code. I don’t like the idea very much but still, we can always have one more wrapping module e.g.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyApp.GithubAPI do
  def impl(), do: # fetch from env or w/e.

  def get_forks(repo) do
    MyApp.GithubClient.get_forks(impl(), repo)
  end
end
</code></pre>
<p>This allows (more) parallel tests as it doesn’t modify the modules globally and also calms down readability fanatics like myself – we don’t have to carry implementations around like the useless friend who always gets drunk and has to be carried / driven home.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="322758" 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/genserver-avoid-refresh-running-in-tests/62603/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-322758" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322758"
                     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="322760" data-post-id="322760">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Req also has test stubs - <a href="https://hexdocs.pm/req/Req.Test.html" class="inline-onebox" rel="noopener nofollow ugc">Req.Test — req v0.6.2</a></p>
<p>But i am not sure what it is exactly that you are trying to test.<br>
Do you want to test the <code>PaymentServer.Exchange.RatesMonitor</code> GenServer?<br>
Or some other parts of your app that depend on this GenServer?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="322760" 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/genserver-avoid-refresh-running-in-tests/62603/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-322760" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="322760"
                     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/62603/load_more?page=3">Load more posts</a>
</div></template></turbo-stream>