brainbag
I am using an Agent for state. Nothing I have tried for asserting an ArgumentError raised in the Agent has worked! The test fails and the process dies without the assertion working.
Here’s some code that demonstrates. In this function add_action, I want to verify that the value of the map key :actions is a list, and hasn’t been changed to something else.
def start_link do
Agent.start_link(@name, :init, [], name: @name)
end
def init do
%{actions: []}
end
def add_action(action) when is_atom(action) do
Agent.get_and_update(@name, fn(map) ->
result = Map.update!(map, :actions, fn
list when is_list(list) -> [action | list]
_ -> raise ArgumentError, message: "key :actions is not a list!"
end)
{result, result}
end)
end
Here is the test:
setup do
{:ok, pid} = State.start_link
[pid: pid]
end
test "action example", context do
# assume State.actions is `42` here instead of `[]`
assert_raise ArgumentError, fn ->
# the process dies here, so `assert_raise` is never called
State.add_action :failure
end
end
I have tried catch_exit, catch_error, catch_throw, catch_pokemon, Process.monitor, receive, etc. No matter what, I still get this error message
=ERROR REPORT==== 1-Oct-2016::19:39:16 ===
** Generic server 'Elixir.ManOrActionMan.State' terminating
** Last message in was {get_and_update,
#Fun<Elixir.DepsInstall.State.0.35286781>}
*snip*
I can’t seem to figure out how to catch that ArgumentError, or if that’s even the right approach to raising errors in a process.
Any help is much appreciated!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
Trapping exits means that the test process won’t be terminated when the linked process terminates. The call to
State.add_action :failurewill still fail though so you’ll need tocatch_exitit.Btw, I am also wondering why are you getting exits formatted using the Erlang logger instead of the Elixir one in your initial report.
Also Liked
brainbag
Thanks @JEG2 and @josevalim by your powers combined I am Captain Solution! The tests are passing now.
In case anyone else ever stumbles through this, here’s my final(?) working test:
@josevalim Your message prompted me to remember that I removed
:loggerfrommix.exsa while ago as a newbie mistake; putting it back changed the Erlang error to an Elixir one. I still see an error print out during the test run, but it passes. I’m sure it’s my fault somehow.josevalim
Great! Btw, adding “@tag :capture_log” before the test should silence the
log reports.
JEG2
I cracked up at
catch_pokemon.You can’t catch the error because it doesn’t happen in the test process. Test it as follows:
assert_receivedon the{:EXIT, …}messageHope that helps.
Last Post!
pradeep-in
Any way to test similar things for non-linked processes? Like tasks under a different supervisor, so no way to trap exits. How can we ensure tests in those scenerios?