Dealing with Task's when testing

I currently have a function which kicks off a Task which updates an Ecto record. When testing this function the Task will start but the record will be deleted before the Task does anything. As a result, I keep getting Ecto.StaleEntryError. The tests themselves don’t fail but stack traces show up in the test logs.

What’s the best way to handle this?

I use a the application env to replace the task supervisor module with this one:

defmodule Connect.TestTaskSupervisor do
  def async_nolink(_, fun), do: fun.()
  def start_child(_, fun), do: fun.()

# Use it like that:
@task_supervisor Application.get_env(:myapp, :task_supervisor) || Task.Supervisor
def todo_async do
  @task_supervisor.start_child(MyApp.Todo, fn -> todo() end)

This does the trick!

Are there any consequences doing it this way? Am I going to run into problems when unit testing the task?