Capturing Logs in Tests

Deankinyua

Deankinyua

Created 5 hours ago

Sometimes when you writing functions that may produce errors, you might want to log the error in the terminal :

    case process_with_flame(id, url, external_id) do
      {:ok, audio_url} ->
        TranscribingWorker.enqueue(%{"id" => id, "audio_url" => audio_url})
        :ok

      {:error, reason} ->
        Logger.error("Failed to process episode: #{id}, reason: #{reason}")
        {:error, reason}
    end

These logs will persists in tests. You can capture them so that they don’t clutter your terminal. To do this tag every test that produces a log with :capture_log. Tags are used to pass information from the test to the callback.

    @tag :capture_log
    test "does not enqueue a transcribing job if uploading process is unsuccessful" do
      expect(MockDownloader, :download, fn _url, _video_path ->
        {:ok,
         "/var/folders/2w/T/012eb1cb-5b41-405f-bcab-7a5236eee471a909da70-13b7-4717-b1c0-c2d001521dc3.mp4"}
      end)
           ....
    end