Testing Elixir: logic error in code sample: with_authenticated_user/1 (Page 25)

The with_authenticated_user/1 code sample on PDF page 25 in B2.0 returns the user object, and not the authenticated_user object as I expect it should. Leaving the code as-is would probably be considered a logic error in a real application, and may be confusing to some readers of the book.

Current code:

def with_authenticated_user(context) do
    user = User.create(%{name: "Bob Robertson"})
    authenticated_user = TestHelper.authenticate(user)

    Map.put(context, :authenticated_user, user) # Error here
end

Fixed code:

def with_authenticated_user(context) do
    user = User.create(%{name: "Bob Robertson"})
    authenticated_user = TestHelper.authenticate(user)

    Map.put(context, :authenticated_user, authenticated_user) # Fixed
end
2 Likes