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: assertions | Hex
Assertions — Assertions v0.15.0

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

Last Post!

devonestes

devonestes

This was a good idea! I’ve added it to the new version (0.15.0) and just published it to hex: assertions | Hex
Assertions — Assertions v0.15.0

Where Next?

Popular in Questions Top

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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New