I was using a float for prices but switched to Money, and although the show pages (and all other CRUD pages) are working, the mix-generated “shows chosen resource” test is now failing.
defmodule AppName.Admin.Product.MaterialControllerTest do
use AppName.ConnCase
alias AppName.Product.Material
@valid_attrs %{name: "340gsm Mesh PVC", price_sq_m: "120.5"}
@invalid_attrs %{}
...
test "shows chosen resource", %{conn: conn} do
material = Repo.insert! %Material{}
conn = get conn, admin_material_path(conn, :show, material)
assert html_response(conn, 200) =~ "Show material"
end
1) test shows chosen resource (AppName.Admin.Product.MaterialControllerTest)
test/controllers/admin/product/material_controller_test.exs:30
** (FunctionClauseError) no function clause matching in Money.to_string/2
stacktrace:
lib/money.ex:394: Money.to_string(nil, [symbol: true])
(app_name) web/templates/admin/product/material/show.html.eex:12: AppName.Admin.Product.MaterialView."show.html"/1
(app_name) web/templates/layout/app.html.eex:29: AppName.LayoutView."app.html"/1
(phoenix) lib/phoenix/view.ex:335: Phoenix.View.render_to_iodata/3
(phoenix) lib/phoenix/controller.ex:642: Phoenix.Controller.do_render/4
(app_name) web/controllers/admin/product/material_controller.ex:1: AppName.Admin.Product.MaterialController.action/2
(app_name) web/controllers/admin/product/material_controller.ex:1: AppName.Admin.Product.MaterialController.phoenix_controller_pipeline/2
(app_name) lib/app_name/endpoint.ex:1: AppName.Endpoint.instrument/4
(app_name) lib/phoenix/router.ex:261: AppName.Router.dispatch/2
(app_name) web/router.ex:1: AppName.Router.do_call/2
(app_name) lib/app_name/endpoint.ex:1: AppName.Endpoint.phoenix_pipeline/1
(app_name) lib/app_name/endpoint.ex:1: AppName.Endpoint.call/2
(phoenix) lib/phoenix/test/conn_test.ex:224: Phoenix.ConnTest.dispatch/5
test/controllers/admin/product/material_controller_test.exs:32: (test)
web/templates/admin/product/material/show.html.eex:
<h2>Show material</h2>
<ul>
<li>
<strong>Name:</strong>
<%= @material.name %>
</li>
<li>
<strong>Price per m²:</strong>
<%= Money.to_string(@material.price_sq_m, symbol: true) %> <!-- line 12 -->
</li>
</ul>
<%= link "Edit", to: admin_material_path(@conn, :edit, @material) %>
<%= link "Back", to: admin_material_path(@conn, :index) %>
I (probably mistakenly) thought this may be because I need to update the @valid_attrs
:price_sq_m
value from "120.5"
to a new Money
, but when I update to the following:
@valid_attrs %{name: "340gsm Mesh PVC", price_sq_m: Money.new(120.5)}
I get:
$ mix test
** (FunctionClauseError) no function clause matching in Money.new/2
lib/money.ex:72: Money.new(120.5, :GBP)
test/controllers/admin/product/material_controller_test.exs:6: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:363: Code.require_file/2
(elixir) lib/kernel/parallel_require.ex:57: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5
18:23:31.449 [error] GenServer #PID<0.176.0> terminating
** (FunctionClauseError) no function clause matching in Money.new/2
lib/money.ex:72: Money.new(120.5, :GBP)
test/controllers/admin/product/material_controller_test.exs:6: (module)
(elixir) src/elixir_compiler.erl:125: :elixir_compiler.dispatch_loaded/6
(elixir) src/elixir_module.erl:192: :elixir_module.eval_form/6
(elixir) src/elixir_module.erl:82: :elixir_module.do_compile/5
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) src/elixir.erl:223: :elixir.erl_eval/3
(elixir) src/elixir.erl:211: :elixir.eval_forms/4
(elixir) src/elixir_compiler.erl:66: :elixir_compiler.eval_compilation/3
(elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/3
(elixir) src/elixir_compiler.erl:30: :elixir_compiler.quoted/3
(elixir) lib/code.ex:363: Code.require_file/2
(elixir) lib/kernel/parallel_require.ex:57: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5
This test is the one remaining thing in my little CRUD and read-only test project that I’d like to fix before starting a course/book on Elixir, so if anyone has any pointers that would be grand. It is only a test, though, so this isn’t holding me up. Thanks!