elixir - Phoenix Framework: What's the correct way to seed database for testing? -


i building first phoenix app , trying write test verify user retrieved jwt in connection's authentication header correct user, authenticated, etc.

what correct way seed database single record test?

should done:

  • globally, using test/test_helper.exs file, requiring priv/repo/seeds.exs , executing manual ecto operations

or

  • per test (in case not how best proceed)

finally, correct way clean out test database after running tests, can avoid trying create same record every time run test?

usually best way keep tests separated each other , init per test. tests run inside ecto transactions rolled @ end , there no junk left. allows running tests in parallel. means user has inserted @ beginning of every test manually. if have more tests depend on user , current session can extract common code tag.

usually data logged in user kept in conn.assigns. lets pretend under key :user.

inside app.conncase setup function add this:

setup %{conn: conn} = config   my_user_email = config[:logged_as]   cond     my_user_email ->       {:ok, user} = repo.insert(...)       conn_with_user = assign(conn, :user, user)       {:ok, conn: conn_with_user, user: user}   end 

and in tests can match on both conn , new account:

@logged_as "user.userowski@company.com" test "jwt works correctly", %{conn: conn, user: user}   # user in db   # don't have retrieve it, because passed test setup   # can start testing jwt  end 

this method described in programming phoenix. recommend book!


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -