otuv

otuv

Testing for expected failure

Hi,

How would one make a test that asserts that the tested method is invalid?

If i for example want to test the following (somewhat contrived) method:

def only_valid_number(number) when is_number number do
  number
end

A simple success test is trivial:

assert 5 == only_valid_number(5)

But what if I want to verify that:

assert ‘got stacktrace’ == only_valid_number(“5”)

Where I don’t really care why, just that any attempts to use it in this way will fail.

Marked As Solved

arjan

arjan

You could use flunk instead to check that the execution does not reach a certain point:

try do
  only_valid_number("x")
  flunk("this should not have happened")
catch
  _, _ -> :ok 
end

Also Liked

devonestes

devonestes

This was a good idea! I’ve added it to the new version (0.15.0) and just published it to hex: https://hex.pm/packages/assertions
https://hexdocs.pm/assertions/0.15.0/Assertions.html#assert_raise/1

arjan

arjan

My suggestion was not entirely right. You could write a expect_failure macro like this:

defmodule ExpectFailure do
  defmacro expect_failure(ast) do
    quote do
      try do
        unquote(ast)
        flunk("This should has crashed: " <> unquote(Macro.to_string(ast)))
      rescue
        e in ExUnit.AssertionError ->
          raise e

        _ ->
          :ok
      end
    end
  end
end

defmodule AsdfTest do
  use ExUnit.Case
  doctest Asdf
  import ExpectFailure

  def foo(x) when is_number(x) do
    x + 1
  end

  test "expect_failure catches errors" do
    expect_failure(foo("a"))
  end

  test "expect_failure should crash" do
    assert_raise ExUnit.AssertionError, fn ->
      expect_failure(foo(1))
    end
  end
end

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement