Test samples generated with mix phx.gen.json

I’m trying to run the test samples generated with mix phx.gen.json. However two of the tests are failing. They are:

  1. test does not create todos and renders errors when data is invalid (Todos.Web.TodosControllerTest)

  2. test does not update chosen todos and renders errors when data is invalid (Todos.Web.TodosControllerTest)

Both are complaining that (UndefinedFunctionError) function Todos.To.Todos.Web.FallbackController.init/1 is undefined (module Todos.To.Todos.Web.FallbackController is not available). But I don’t understand the call to Todos.To.Todos.Web.FallbackController.init/1 when the code should be calling Todos.Web.FallbackController.

Any help from the community in understanding this will be appreciated.

Update1: When I created a module Todos.To.Todos.Web.FallbackController, the two tests passed. Does this mean I will have to create such module for every context to get tests that need the action fallback to pass?

it’s weird that you ended up with Todos.To.Todos.Web namespace, perhaps you incorrectly called one of the generators? I’d expect simply: Todos.Web. Do you recall the exact command you run with mix phx.gen.json or any other previous generator commands?

Thanks @wojtekmach This is the command I ran mix phx.gen.json To Todos todos title:string description:string. So I got Todos.To for the api and Todos.To.Todos for the schema, including Todos.Web.FallbackController for the action fallback. But somehow the test expects a Todos.To.Todos.Web.FallbackController module. May be the generated test samples don’t work well with json context. I tried phx.gen.html earlier and all tests passed and so I decided to checkout the the json.

Ok I think it might be a name collision issue probably because my top level module is named Todos and I had a nested module Todos.To.Todos. I generated another app without name collision and everything works fine. Thanks @wojtekmach.

1 Like