Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix. Mock not working for grandchildren and beyond #746

Merged

Conversation

carrascoacd
Copy link
Contributor

Context

!is_nil(Process.get(ancestor, __MODULE__)) always returns true, hence the find fn returns the first ancestor.

Solution

Iterate properly over ancestors trying to find the __MODULE__ key.

cc/ @yordis

@carrascoacd carrascoacd force-pushed the acc/fix-tesla-mock-for-grandchildren branch from 85bb698 to 0cd470b Compare January 24, 2025 13:01
Copy link
Member

@yordis yordis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@yordis yordis merged commit 026476d into elixir-tesla:master Jan 24, 2025
6 checks passed
@tomekowal
Copy link
Contributor

Hey! Thanks for that fix. I was bitten by the same issue recently.
However, I am wondering if the code works as intended.
E.g. it starts searching for the mock from the "oldest" or "most root" ancestor.
If, for some reason, parent process defined a mock, then defining new one in the test process would not work.
Also, I don't think $ancestors is the correct thing to use.
E.g. for supervised tasks, the ancestor is the task supervisor and not the test process.

I believe, something like this should work better:

pids = [self() | Enum.reverse(Process.get(:"$callers", []))]

pid_holder = Enum.find(pids, nil, fn pid -> ... end)

If you agree with my reasoning, I am happy to make a PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants