View Source Uploadex.TestStorage (Uploadex v3.1.0)

Storage to be used for testing. It holds the files in memory (using a Agent).

Configuring

To configure your app to use the Uploadex.TestStorage, just add this storage to your Uploadex module, depending on the current environment:

defmodule MyApp.Uploader do
  use Uploadex

  @impl true
  def get_fields(%User{}), do: :files

  @impl true
  def default_opts(Uploadex.FileStorage), do: [...]
  def default_opts(Uploadex.TestStorage), do: []

  @impl true
  def storage(%User{}, _field) do
    if test_environment?() do
      {Uploadex.TestStorage, []}
    else
      {Uploadex.FileStorage, [...]}
    end
  end

  @impl true
  def accepted_extensions(%User{}, _field), do: ~w(.jpg .png)

  defp test_environment?() do
    # This env must be set in your config files depending on the environment.
    Application.fetch_env!(:my_app, :environment) == :test
  end
end

Using

In your ExUnit tests, add a setup block starting the storage:

setup do
  Uploadex.TestStorage.start_link()
  :ok
end

Then, in the tests, you can use the get_stored/1, get_deleted/1 and get_opts:

test "some test with files" do
  assert ["document-1.pdf", "document-2.pdf"] == Uploadex.TestStorage.get_stored()
  assert ["deleted-document.pdf"] == Uploadex.TestStorage.get_deleted()
  assert [] == Uploadex.TestStorage.get_opts()
end

This module is just the base for testing, check Uploadex.Testing for a more convenient way to test uploads.

Summary

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

start_link(initial_state \\ %{})

View Source