ex-unit

Elixir assert_raise doesn't catch exceptions

只谈情不闲聊 提交于 2020-02-03 03:56:45
问题 I wrote this test case: assert_raise ArgumentError, myFn(a,b) but it does not evaluate in the way I'd expect. myFn raises an ArgumentError ( do: raise ArgumentError ), but it is not caught by assert_raise . The example in the docs works just fine: assert_raise ArithmeticError, fn -> 1 + "test" end From the documentation: assert_raise(exception, function) Asserts the exception is raised during function execution. Returns the rescued exception, fails otherwise I'm guessing that in my test case,

Elixir doctest fails for function that returns random values

萝らか妹 提交于 2019-12-24 01:07:05
问题 I have a function in Elixir that produces three random RGB tuples in a list. defmodule Color do @doc """ Create three random r,g,b colors as a list of three tuples ## Examples iex> colors = Color.pick_color() iex> colors [{207, 127, 117}, {219, 121, 237}, {109, 101, 206}] """ def pick_color() do color = Enum.map((0..2), fn(x)-> r = Enum.random(0..255) g = Enum.random(0..255) b = Enum.random(0..255) {r, g, b} end) end When I run my tests, my doctests fail. The resulting list of tuples are

How to use connection with session in phoenix?

為{幸葍}努か 提交于 2019-12-08 15:05:34
问题 I have an authentication plug and I want to test my controllers. The problem is that the line in this plug has user_id = get_session(conn, :user_id) And it's always nil when I'm using this method (I used dirty hack before, but I no longer want to do it): @session Plug.Session.init([ store: :cookie, key: "_app", encryption_salt: "secret", signing_salt: "secret", encrypt: false ]) user = MyApp.Factory.create(:user) conn() |> put_req_header("accept", "application/vnd.api+json") |> put_req_header

How to run ExUnit tests within IEx

♀尐吖头ヾ 提交于 2019-12-06 21:21:21
问题 I'm trying to launch IEx.pry within a test. However I cannot get to run the tests within an iex session. Note that I'm not using mix. ExUnit.start defmodule Calc do def add(a,b) do a + b end end defmodule TheTest do use ExUnit.Case test "adds two numbers" do require IEx IEx.pry assert Calc.add(1, 2) == 3 end end I try run it with ExUnit.run hangs and eventually times out: manuel@laptop:~/exercism/elixir/nucleotide-count$ iex test.exs Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async

How to run ExUnit tests within IEx

家住魔仙堡 提交于 2019-12-05 00:44:43
I'm trying to launch IEx.pry within a test. However I cannot get to run the tests within an iex session. Note that I'm not using mix. ExUnit.start defmodule Calc do def add(a,b) do a + b end end defmodule TheTest do use ExUnit.Case test "adds two numbers" do require IEx IEx.pry assert Calc.add(1, 2) == 3 end end I try run it with ExUnit.run hangs and eventually times out: manuel@laptop:~/exercism/elixir/nucleotide-count$ iex test.exs Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false] Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for

How do I fake IO input when testing with ExUnit?

徘徊边缘 提交于 2019-12-04 03:31:31
问题 I have an Elixir program I'd like to test which gets input from the user via IO.gets several times. How would I go about faking this input in a test? Note: I would like to return a different value for each IO.gets 回答1: The preferred way to do it is to split your code into pure (no side effects) and impure (does the io). So if your code looks like this: IO.gets ... ... ... IO.gets ... ... try to extract the parts between IO.gets into functions that you can test in isolation from IO.gets : def

How do I fake IO input when testing with ExUnit?

自古美人都是妖i 提交于 2019-12-01 19:14:57
I have an Elixir program I'd like to test which gets input from the user via IO.gets several times. How would I go about faking this input in a test? Note: I would like to return a different value for each IO.gets tkowal The preferred way to do it is to split your code into pure (no side effects) and impure (does the io). So if your code looks like this: IO.gets ... ... ... IO.gets ... ... try to extract the parts between IO.gets into functions that you can test in isolation from IO.gets : def fun_to_test do input1 = IO.gets fun1(input1) input2 = IO.gets fun2(input2) end and then you can test