unlogic

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

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

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

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.

Where Next?

Popular in Questions Top

qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

We're in Beta

About us Mission Statement