soullpoint

soullpoint

How are functions scoped within ExUnit describe blocks?

Hi,

I was wondering if anyone could help clarify something for me. This is more due to my lack of understanding than anything else.

Suppose I had a module for testing, with two describe blocks. I declare a function with the same name in both, one with defaults, and one without. Perhaps owing to my OO background, I naively expected that these functions are in different scopes and therefore wouldn’t affect each other:

defmodule Sample.SampleTest do
  ...

  describe "test case A" do
    alias AnotherModule
    def helper_function(attrs \\ %{}) do
      ...
    end
  end

  describe "test case B" do
    alias AnotherModule
    def helper_function() do
      ...
    end
  end
end

However, I am getting an error that helper_function/0 conflicts with defaults from helper_function/1.

I presume that the functions are therefore scoped to the containing module? But then, I also have aliases within the describe blocks which don’t appear to be shared. (If I don’t include the alias, it fails to compile if I call a function from the module). How are these different?

If anyone could help me understand this it would be much appreciated.

Marked As Solved

soullpoint

soullpoint

To answer my own question, the function is not “scoped” to the describe, because as dimitarvp mentioned, describe does not define a new module scope.

The alias however, is scoped to the nearest block, and the describe is one such construct, so the alias is not “shared” between the two blocks.

Just trying to understand the fundamentals. I feel like this makes sense, but please feel free to correct me if I’m wrong!

Also Liked

dimitarvp

dimitarvp

AFAIK, neither describe nor test define a new module scope (or class as you’d call it). So what you are trying to do will not work. What’s the problem with having functions with different names at the top scope in the same file?

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement