alias problem with callbacks
smpallen99 opened this issue · 1 comments
My tests were working fine on 0.1.2. Upgrade to 0.2.2 has compile issues when using aliases in callbacks.
The following test runs on 0.1.2, but gives the following compile error on 0.2.2:
==> example
Compiled lib/example.ex
Generated example.app
** (CompileError) nofile:7: One.__struct__/0 is undefined, cannot expand struct One
(elixir) src/elixir_map.erl:44: :elixir_map.translate_struct/4
(stdlib) lists.erl:1352: :lists.mapfoldl/3
(stdlib) lists.erl:1353: :lists.mapfoldl/3
# lib/example.ex
defmodule Example do
end
defmodule Example.One do
defstruct one: 1
def get, do: inspect(__MODULE__)
end
# test/example_test.exs
defmodule ExampleTest do
use Pavlov.Case, async: true
alias Example.One
before :each do
{:ok, one: %One{one: 3}}
end
it "gets Example.One" do
assert One.get == "Example.One"
end
describe "nested" do
end
end
Ah, yes, I've also been seeing this in my projects since the upgrade. The way to get deep callbacks and lets was to simply store these functions and then redeclare them inside each child module created by describe
or context
. This works, but apparently Elixir doesn't "remember" the scope that the function was originally declared under, leading to the problem you're seeing with aliases. I've been trying to figure out a way to store and pass this context along, with no luck so far.
The workaround I found was to either stop using aliases altogether (and use the fully qualified module name) or to do import MyModule
inside the callback or let expression.
I'll leave the issue open nonetheless, but maybe you can use the workarounds to get your tests passing in the meantime?
Thanks!