High level testing in isolation

Hello,

My application consists of a couple of gen servers that all keep some sort of state:

  • EntranceHandler: Polls an endpoint every x seconds and stores the result in an ets table, it’s supervised so when it crashes the ownership of the table is transfered to the new started child.
  • Settings: Small genserver that keeps configuration in an ets table

For my unit tests I could easily start an EntranceHandler genserver with the context.test as name and inject it in the different places but for my controller specs and integration specs this is not possible. By default the GenServers register themself under the __MODULE__ name if no name is given. I also have different mocks (Agents with same behaviour implemented) when I need a dependent Genserver. Those mock agents also register themself as name __MODULE__.

Different controllers interact with Settings redirect back to the configuration page if the Settings are not configured yet. When running the specs async / concurrently this means that one spec might clear the Settings mock while another one just asserts that the settings are valid. This results in a lot of random failures.

My question is how can I test different controllers correctly in isolation?

If you really want to run integration tests in isolation then you’d likely want to start them independent of the global name registration. Take a look at https://github.com/plataformatec/mox and jose’s explanations here: Struggling with the Mock noun

Otherwise you cannot use async tests and would do the setup/cleanup for each test.