Need another pair of eyes on assert_raise

79 views
Skip to first unread message

Booker Bense

unread,
Apr 16, 2015, 11:44:10 AM4/16/15
to elixir-l...@googlegroups.com
There's obviously something here I am completely missing about how to use assert_raise. 

I have this code: 

defmodule RaiseTest do

        def testraise do
                raise "This is an error"
        end
end

and I test it with this test

defmodule RaiseTestTest do
  use ExUnit.Case

  test "assert_raise works" do
    assert_raise(RuntimeError, RaiseTest.testraise )
  end
end

When I run mix test I get these results

mix test


  1) test assert_raise works (RaiseTestTest)
     test/raise_test_test.exs:4
     ** (RuntimeError) This is an error
     stacktrace:
       (raise_test) lib/raise_test.ex:4: RaiseTest.testraise/0
       test/raise_test_test.exs:5



Finished in 0.03 seconds (0.03s on load, 0.00s on tests)
1 tests, 1 failures

Shouldn't this test pass rather than fail?

- Booker C. Bense

José Valim

unread,
Apr 16, 2015, 12:00:17 PM4/16/15
to elixir-l...@googlegroups.com
You need to pass a function to assert_raise. From the docs:

┃ assert_raise ArithmeticError, "bad argument in arithmetic expression", fn ->
┃   1 + "test"
┃ end




José Valim
Skype: jv.ptec
Founder and Lead Developer

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/d986a671-6d05-4ae3-b0ba-ff8b489bddd8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Booker Bense

unread,
Apr 16, 2015, 2:14:44 PM4/16/15
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Doh! 


On Thursday, April 16, 2015 at 9:00:17 AM UTC-7, José Valim wrote:
You need to pass a function to assert_raise. From the docs:

┃ assert_raise ArithmeticError, "bad argument in arithmetic expression", fn ->
┃   1 + "test"
┃ end

Is it just me, or is the use of "function" in this case confusing? 

My mental error seems to be confusing 

Module.function(foo) 

which is actually what ever the "function" returns and not a function per se with
a "real" function that can be passed around. In C speak, the argument required
is a function reference. Now that I think about it, this is a problem I've run into 
a lot and more or less cargo-culted the right code without thinking what I am really
doing. 

I guess I just need to remap my mental landscape. 

thanks, 

- Booker C. Bense 
Reply all
Reply to author
Forward
0 new messages