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
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
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?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









