Skip to content

Possibility to give forall a cleanup hook? #169

@turion

Description

@turion

Often, I encounter the following bug in my tests:

property "test database", [], ctx do
  forall row_to_be_inserted = %schema{} <- gen_row() do
    Ecto.insert(row_to_be_inserted, ctx.repo)
    match([%schema{}], Ecto.all(schema))
  end
end

The bug is that upon multiple iterations of forall, the database is polluted with test data and the property fails because of side effects introduced in earlier iterations.

One possibility is to clean up manually like this:

property "test database", [], ctx do
  forall row_to_be_inserted = %schema{} <- gen_row() do
    Ecto.delete_all(schema)
    Ecto.insert(row_to_be_inserted, ctx.repo)
    match([%schema{}], Ecto.all(schema))
  end
end

But this is tedious when doing it in many similar tests, especially if the database setup changes. It would be nice if there was a hook similar to ExUnit's on_exit which is called at the end or at the beginning of every forall in every property, and can be configured per module. This hook could then do the cleanup in one central place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions