jxxcarlson

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

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You can’t call functions from within the module body that defines them.

Azolo

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

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. :slight_smile:

The ‘test’ cases are basically (are, not just basically) functions, hence why you can call it in those.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

jxxcarlson

Aha! that explains it!

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement