I’m using ex_machina to create test fixture data in my test spec.
It works fine to create data in my database and serve my test.
However, I would like to have a clean test database after each test.
In Ruby, I would use database_cleaner to do so. I wonder if there is any package in Elixir which is similar to that. (I have tried Google it and no luck).
My question is what is the idiomatic way to clean up database?
Don’t the Ecto test functions operate in a sandbox? So it uses existing pre-filled data (or none if you want), and everything in each test is run in its own little sandbox? Thus I have no clue what a database_cleaner would be needed for? Or does it do something other than what the name implies?
Do I have to setup my test suit so Ecto will run in sandbox mode? Or it’s default in test mode.
After my test was ran, the test data always persisted in my test database.
So I suspect I did not setup my test suit correctly. However I cannot figure it out.
Here is my test
test "a person exists returns facebook person" do
# TODO: Cleanup data after each test.
insert(:test_data)
conn = conn(:get, "test_url")
|> use_basic_auth(@username, @password)
|> Myapp.call(@opts)
# Assert the response and status
assert conn.state == :sent
assert conn.status == 200
{:ok, data} = Poison.decode(conn.resp_body)
assert data["id"] == "id"
assert data["type"] == "person"
end
defmodule Factory do
use ExMachina.Ecto, repo: UserStoreEx.Repo
def user_store_data_factory do
%MyApp.MyData {
id: "id",
type: "person",
data: "{}"
}
end
end
I used ex_machina to generate test fixture. Is there anything to deal with that?
I stumbled around trying to get this to work and finally did. Seems like maybe I needed to run MIX_ENV=test mix clean after getting all these settings right? Does that even make sense?
Theoretically you never need to run mix clean but, if you did and it fixed things, it is because something left some dirty state behind (which it was not supposed to). A way to reproduce the dirty state would be appreciated but I realize it may take a lot of time to do so.