Skip to content

Commit

Permalink
Mock Onigumo.Downloader in CLI tests
Browse files Browse the repository at this point in the history
Onigumo.Component is a behavior that can be mocked. CLI tests don’t
verify behavior of concrete components. Defined a mock for the
Downlaoader components so the CLI tests only verifies its call, not
its behavior.

Defined the expect mock to return the passed working directory to test
it’s set properly.
  • Loading branch information
Glutexo committed Apr 6, 2024
1 parent 32ec35a commit c8d714d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Config

config(:onigumo, :http_client, HTTPoison)
config(:onigumo, :downloader, Onigumo.Downloader)
1 change: 1 addition & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Config

config(:onigumo, :http_client, HTTPoisonMock)
config(:onigumo, :downloader, OnigumoDownloaderMock)
2 changes: 1 addition & 1 deletion lib/cli.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Onigumo.CLI do
@components %{
:downloader => Onigumo.Downloader
:downloader => Application.compile_env(:onigumo, :downloader)
}

def main(argv) do
Expand Down
4 changes: 3 additions & 1 deletion test/onigumo_cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ defmodule OnigumoCLITest do
test("run CLI with 'downloader' argument", %{tmp_dir: tmp_dir}) do
expect(HTTPoisonMock, :start, fn -> nil end)
expect(HTTPoisonMock, :get!, length(@urls), &HttpSupport.response/1)
expect(OnigumoDownloaderMock, :main, fn root_path -> root_path end)

input_path_env = Application.get_env(:onigumo, :input_path)
input_path_tmp = Path.join(tmp_dir, input_path_env)
input_file_content = InputSupport.url_list(@urls)
File.write!(input_path_tmp, input_file_content)
File.cd(tmp_dir)
Onigumo.CLI.main(["downloader"])

assert Onigumo.CLI.main(["downloader"]) == tmp_dir
end

for argument <- @invalid_arguments do
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ExUnit.start()

Mox.defmock(HTTPoisonMock, for: HTTPoison.Base)
Mox.defmock(OnigumoDownloaderMock, for: Onigumo.Component)

0 comments on commit c8d714d

Please sign in to comment.