jxxcarlson
Getting undefined function error - but it is present
I’m getting a function undefined error that totally mystifies me:
describe "sessions" do
alias Koko.Authentication.Session
@valid_user_attrs %{"admin" => true, "blurb" => "BLURB!", "email" => "yada@foo.io",
"name" => "Yada T. Urdik", "username" => "yada", "password" => "abc.617.ioj"}
def user_fixture2(attrs \\ %{}) do
{:ok, user} =
attrs
|> Enum.into(@valid_user_attrs)
|> Authentication.create_user()
user
end
user = user_fixture2(%{})
#ETC.
Then
** (CompileError) apps/koko/test/koko/authentication/authentication_test.exs:94:
undefined function user_fixture2/1
Marked As Solved
Ankhers
Alright. I am not entirely sure the reason behind it, but it turns out you cannot call the function in the describe/2 block, but you can call it in the test/1 block. I am not really sure of the limitations on this because I was able to call List.first/1 without an issue. So maybe it is limited to functions defined in the same exs file?
Either way, what you should be doing is using a setup_all/1 block in order to do something for all of the tests within the describe/2.
Also Liked
benwilson512
You can’t call functions from within the module body that defines them.
Azolo
There’s a better way to explain what I’m about to say, but you’re trying to assign a variable in the AST using a method that’s also a leaf in the same tree.
You will have to declare the method in a new module entirely or use it in a test or setup block of that module.
OvermindDL1
An example, they are saying that you are trying to do something like this:
iex(2)> defmodule Testering do
...(2)> def blah(i), do: i * 2
...(2)> bloop = blah(21)
...(2)> end
** (CompileError) iex:4: undefined function blah/1
You are trying to use a function that is not compiled yet is all. 
The ‘test’ cases are basically (are, not just basically) functions, hence why you can call it in those.
benwilson512
Yup. You can tell because it lets you set module attributes in the describe block, which you can’t do inside of a function.
jxxcarlson
Aha! that explains it!








