unlogic
Unit test and runtime behaviour of controller differ
I have a Phoenix application that is provides a standard REST API. I currently have two models and am looking at unit-testing. One of my controller’s unit tests is passing fine, but the other is showing some off behaviour in the update and create tests.
- Update with valid data isn’t updating the data and returns
{"errors":{"comment":["can't be blank"],"control":["can't be blank"],"value":["can't be blank"]}} - Update with invalid data isn’t erroring and returning the entry
- Create with valid data is doing what the invalid case should do and returning 422 with
{"errors":{"comment":["can't be blank"],"control":["can't be blank"],"value":["can't be blank"]}}
These are generated tests, as are those for the other controller which are working fine. I’d be OK with this if the controller wasn’t working as expected, but when running the queries manually from a REST client, the application behaves as expected in all three cases.
Here’s the
- unit test https://www.paste.in/aIwZpZ
- Controller https://www.paste.in/LFb2CW
- Model https://www.paste.in/XFpqij
Note that I have dropped the ["data"] part of the response. This is the case for the other controller too, where the tests are passing fine.
Marked As Solved
al2o3cr
def create(conn, job_control_params) do
Try dumping out what you’re getting in job_control_params here when you run the tests. Does it match what JobControls.create_job_control/1 expects?
I don’t believe it will: create_job_control is called from fixture(:job_control) with parameters shaped like
%{comment: ..., control: ..., value: ...}
but in the tests this code:
test "renders job_control when data is valid", %{conn: conn} do
conn = post(conn, Routes.job_control_path(conn, :create), job_control: @create_attrs)
will pass (unless use FarmqWeb, :controller is doing something nonstandard) parameters shaped like:
%{"job_control" => %{"comment" => ..., "control" => ..., "value" => ...}}
cast will ignore parameters it doesn’t understand, so it will filter out ALL the input here.
A typical idiom in controller actions looks like:
def create(conn, %{"job_control" => job_control_params}) do
with {:ok, %JobControl{} = job_control} <- JobControls.create_job_control(job_control_params) do
...
def update(conn, %{"id" => id, "job_control" => job_control_params}) do
job_control = JobControls.get_job_control!(id)
with {:ok, %JobControl{} = job_control} <- JobControls.update_job_control(job_control, job_control_params) do
Also Liked
al2o3cr
Can you post the controller code? The link labeled “Controller” above is actually a schema module.
I have zero evidence to support this, but my suspicion is that the controller doesn’t agree with the test about what key to use in the params.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










