phoenixframework / phoenix

Peace of mind from prototype to production
https://www.phoenixframework.org
MIT License
21.4k stars 2.87k forks source link

Tests fail after phoenix.gen.json #1048

Closed willrax closed 9 years ago

willrax commented 9 years ago

Hi,

After starting a new project with phoenix and running

mix phoenix.gen.json Saying sayings name:string body:string

running mix test results in the following:

  1) test deletes chosen resource (Echo.SayingControllerTest)
     test/controllers/saying_controller_test.exs:50
     ** (Protocol.UndefinedError) protocol Phoenix.Param not implemented for {:ok, %Echo.Saying{__meta__: %Ecto.Schema.Metadata{source: {nil, "sayings"}, state: :loaded}, body: nil, id: 6, inserted_at: #Ecto.DateTime<2015-07-23T10:13:28Z>, name: nil, updated_at: #Ecto.DateTime<2015-07-23T10:13:28Z>}}
     stacktrace:
       (phoenix) lib/phoenix/param.ex:122: Phoenix.Param.Any.to_param/1
       (echo) web/router.ex:1: Echo.Router.Helpers.saying_path/4
       test/controllers/saying_controller_test.exs:52

Here are my deps.

  defp deps do
    [{:phoenix, "~> 0.13.1"},
     {:phoenix_ecto, "~> 0.4"},
     {:postgrex, ">= 0.0.0"},
     {:phoenix_html, "~> 1.0"},
     {:phoenix_live_reload, "~> 0.4", only: :dev},
     {:cowboy, "~> 1.0"}]
  end

The only thing i've changed is adding the pool to my test config file.

config :echo, Echo.Repo,
  pool: Ecto.Adapters.SQL.Sandbox,
  adapter: Ecto.Adapters.Postgres,
  username: "echo",
  password: "",
  database: "echo_test",
  size: 1 # Use a single connection for transactional tests

Any help is appreciated :)

josevalim commented 9 years ago

Add {:ecto, "~> 0.13.0"} to your mix.exs deps and you should be good to go.

josevalim commented 9 years ago

The reason being you are using a too recent ecto version that your phoenix version is not yet compatible.

josevalim commented 9 years ago

Actually, ignore me, update to phoenix to: {:phoenix, "~> 0.14.0"} instead of changing Ecto. It is better than using an old Ecto version.

willrax commented 9 years ago

Ah right. Thanks @josevalim.

fbjork commented 9 years ago

@josevalim I'm still getting this error. I tried upgrading too.

Here's my mix.lock:

%{"cowboy": {:hex, :cowboy, "1.0.2"},
  "cowlib": {:hex, :cowlib, "1.0.1"},
  "decimal": {:hex, :decimal, "1.1.0"},
  "ecto": {:hex, :ecto, "0.14.1"},
  "fs": {:hex, :fs, "0.9.2"},
  "phoenix": {:hex, :phoenix, "0.14.0"},
  "phoenix_ecto": {:hex, :phoenix_ecto, "0.6.0"},
  "phoenix_live_reload": {:hex, :phoenix_live_reload, "0.4.3"},
  "plug": {:hex, :plug, "0.13.0"},
  "poison": {:hex, :poison, "1.4.0"},
  "poolboy": {:hex, :poolboy, "1.5.1"},
  "postgrex": {:hex, :postgrex, "0.9.1"},
  "ranch": {:hex, :ranch, "1.1.0"}}
fbjork commented 9 years ago

Quick update, I tried upgrading to more recent deps, but still same error. Here's the mix.lock file now:

%{"cowboy": {:hex, :cowboy, "1.0.2"},
  "cowlib": {:hex, :cowlib, "1.0.1"},
  "decimal": {:hex, :decimal, "1.1.0"},
  "ecto": {:hex, :ecto, "0.14.3"},
  "fs": {:hex, :fs, "0.9.2"},
  "phoenix": {:hex, :phoenix, "0.14.0"},
  "phoenix_ecto": {:hex, :phoenix_ecto, "0.7.0"},
  "phoenix_live_reload": {:hex, :phoenix_live_reload, "0.4.3"},
  "plug": {:hex, :plug, "0.13.0"},
  "poison": {:hex, :poison, "1.4.0"},
  "poolboy": {:hex, :poolboy, "1.5.1"},
  "postgrex": {:hex, :postgrex, "0.9.1"},
  "ranch": {:hex, :ranch, "1.1.0"}}
josevalim commented 9 years ago

Can you paste your errors? It should be just fine with the reported versions...

José Valimwww.plataformatec.com.br http://www.plataformatec.com.br/Founder and Director of R&D

fbjork commented 9 years ago

Sure, here goes:

1) test does not update chosen resource and renders errors when feed is invalid (FeedService.FeedControllerTest) test/controllers/feed_controller_test.exs:49 \ (Protocol.UndefinedError) protocol Phoenix.Param not implemented for {:ok, %FeedService.Feed{meta: %Ecto.Schema.Metadata{source: {nil, "feeds"}, state: :loaded}, filters: nil, id: "aa716042-f0f9-4437-83f6-0a85db9cd39e", inserted_at: #Ecto.DateTime2015-07-24T01:18:32Z, name: "foo", updated_at: #Ecto.DateTime2015-07-24T01:18:32Z, user_id: "123456"}} stacktrace: (phoenix) lib/phoenix/param.ex:122: Phoenix.Param.Any.to_param/1 (feed_service) web/router.ex:1: FeedService.Router.Helpers.feed_path/4 test/controllers/feed_controller_test.exs:51

josevalim commented 9 years ago

When you upgraded Phoenix, did you run the generator again to update the generated code?

José Valimwww.plataformatec.com.br http://www.plataformatec.com.br/Founder and Director of R&D

willrax commented 9 years ago

@fbjork as @josevalim mentioned, updating the dependencies and then rerunning the generator for my json resource fixed the issue.

Is there an easier way to re run a generator other than deleting the files and rerunning it? I was looking for an overwrite flag but couldn't spot one.

josevalim commented 9 years ago

No, there is no such flag yet afaik.

willrax commented 9 years ago

Ok cool. Thanks for the help :)

fbjork commented 9 years ago

Ah that did it. Adding a bang to Repo.insert! fixed the issues.

maxgronlund commented 9 years ago

I got the same issue

\ (Protocol.UndefinedError) protocol Phoenix.Param not implemented for {:ok, %ElixirFriends.Post{meta: #Ecto.Schema.Metadata<:loaded>, content: "fobar", id: 35, image_url: "http://fc00.deviantart.net/fs71/i/2012/176/8/f/marvin_the_paranoid_android_by_wouthezx-d54uny0.jpg", inserted_at: #Ecto.DateTime2015-08-25T15:38:46Z, source_url: "http://elixirsips.com", updated_at: #Ecto.DateTime2015-08-25T15:38:46Z}}

I'm a totally phoenix newbee and blown away, following along on elixirsips 168: Continuous Integration.

I pulled a pristine version of phoenix. Here is my mix.lock file %{"cowboy": {:hex, :cowboy, "1.0.2"}, "cowlib": {:hex, :cowlib, "1.0.1"}, "decimal": {:hex, :decimal, "1.1.0"}, "ecto": {:hex, :ecto, "1.0.0"}, "fs": {:hex, :fs, "0.9.2"}, "phoenix": {:hex, :phoenix, "0.17.0"}, "phoenix_ecto": {:hex, :phoenix_ecto, "1.1.0"}, "phoenix_html": {:hex, :phoenix_html, "2.1.2"}, "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.0"}, "plug": {:hex, :plug, "1.0.0"}, "poison": {:hex, :poison, "1.5.0"}, "poolboy": {:hex, :poolboy, "1.5.1"}, "postgrex": {:hex, :postgrex, "0.9.1"}, "ranch": {:hex, :ranch, "1.1.0"}}

Phoenix rocks, experienced ROR developer

josevalim commented 9 years ago

@maxgronlund have you just generated the controller or it is an old one?

maxgronlund commented 9 years ago

NOTICE! this was my bad... not a bug in Phoenix

Just generated from a fresh download My report was inaccurate. $ "mix phoenix.server" runs like a charm and opening the page works perfect

$ mix test

/tests/views/post_controller_test.exs

test "shows an image for the post", %{conn: conn} do post = Repo.insert %Post{image_url: "http://fc00.deviantart.net/fs71/i/2012/176/8/f/marvin_the_paranoid_android_by_wouthezx-d54uny0.jpg", source_url: "http://elixirsips.com", content: "fobar"} conn = get conn, post_path(conn, :show, post) <<------- This line throw the error

end

maxgronlund commented 9 years ago

I got it. my bad.. Knewter from Elixir Sips helped me with the following reply Insert now returns {:ok, model}. At the time of this episode it just returned model. Insert! just returns model. So you can either pattern match insert differently or replace it with insert!