PhoenixFlags.Testing (PhoenixFlags v0.11.0)

Copy Markdown View Source

Test helpers for PhoenixFlags.

Process-Scoped Stubs

stub/3 stores a value in the process dictionary, scoped to the calling process. When cache_enabled: false (test env), get/3 checks the process dictionary before falling back to the database.

This avoids DB writes and race conditions in async tests:

setup do
  MyApp.SystemConfig.Test.stub("enable_benefits", true)
  :ok
end

A stub is also seen by processes the test starts with Task -- anything that records the test in $callers -- as Mox's expectations are.

Cross-Process (LiveView) Tests

For LiveView and integration tests where the config is read in a different process, insert a DB row instead. The Ecto sandbox in shared mode handles isolation:

MyApp.SystemConfig.Test.insert_entry("enable_benefits", true)

Summary

Functions

Reads a stub set by this process or by one that started it. Returns {:ok, value} or :none.

Inserts or updates a system flag entry in the database.

Sets a per-process config stub. Only effective when cache_enabled: false.

Functions

get_stub(instance, key)

Reads a stub set by this process or by one that started it. Returns {:ok, value} or :none.

insert_entry(repo, key, value, opts \\ [])

Inserts or updates a system flag entry in the database.

Use this for LiveView/integration tests where the config is read in a different process. The Ecto sandbox in shared mode ensures isolation.

PhoenixFlags.Testing.insert_entry(MyApp.Repo, "enable_benefits", true)
PhoenixFlags.Testing.insert_entry(MyApp.Repo, "max_retries", 5, type: "integer")

Given instance: (which the generated Test.insert_entry/3 passes), the type comes from the flag's declaration, and a :secret value is encrypted with the instance's encryptor, as a real save would. Without it the type is inferred from the value, and a secret cannot be inserted: it would be stored as plaintext and then fail to decrypt.

stub(instance, key, value)

Sets a per-process config stub. Only effective when cache_enabled: false.