Exunit: protocol Enumerable not implemented for %mystruct

I’ve been running into this error in testing and haven’t had much luck debugging so hopefully someone can point me the right way. I’ve been following along with the Rumbl tutorial in Programming Phoenix except that I’ve been using a RESTful service and sending JSON.

I’ve got authentication working using the same session-based system as in Programming Phoenix but some of the tests have been failing.

Elixir version

     Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Elixir 1.2.5

Here is the Exunit output:

      2) test updates and renders chosen resource when data is valid (Jrumbl.UserControllerTest)
 test/controllers/user_controller_test.exs:64
 ** (Protocol.UndefinedError) protocol Enumerable not implemented for %Jrumbl.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">, email: nil, id: 321, inserted_at: #Ecto.DateTime<2016-11-12 14:52:45>, password: nil, password_hash: nil, updated_at: #Ecto.DateTime<2016-11-12 14:52:45>, username: nil}
 stacktrace:
   (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
   (elixir) lib/enum.ex:116: Enumerable.reduce/3
   (elixir) lib/enum.ex:1477: Enum.reduce/3
   (jrumbl) Jrumbl.Router.Helpers.segments/3
   (jrumbl) web/router.ex:1: Jrumbl.Router.Helpers.user_path/3
   test/controllers/user_controller_test.exs:66

And here is the code from the test:

 64  test "updates and renders chosen resource when data is valid", conn do
 65    user = Repo.insert! %User{}
 66    conn = put conn, user_path(conn, :update, user), user: @valid_attrs
 67    assert json_response(conn, 200)["data"]["id"]
 68    assert Repo.get_by(User, @valid_attrs_query)
 69  end

Thanks for the help!

It looks like you are trying to Enum.reduce a %Jrumbl.User{} struct. Structs can’t be reduced like that.

Can you show the code you are calling that is producing the error?

Whups, I forgot the code from the test! I updated the OP with it.

I should also mention that the test was created by the generator.

Can you try user_path(conn, :update, user.id) in the test (if your user path takes user ID as argument)? Looks like the path helper doesn’t like the user struct.

1 Like