Testing Elixir: Test pid confusion (page 28)

The text reads:

Additionally, we have to define the test’s pid outside of the function because self() wouldn’t be evaluated until the function is executed, inside the process of the code under test, which could have a different pid than our test.

But the example code is:

weather_fn_double = fn city ->
    send(self(), {:get_forecast_called, city})
    ...
end

Which looks like the test pid (self()) is defined inside the function. It’s a little confusing, so maybe the text could be improved.

Thanks.

2 Likes

In what appears to be a clarification, the following remains unclear:

Additionally, we have to define the test’s pid outside of the function because self() wouldn’t be evaluated until the function is executed, inside the process of the code under test, which could have a different pid than our test. Because anonymous functions are closures in Elixir, the value of future_unix is part of the function when it gets executed. The call to self() is evaluated at the definition of the anonymous function, with the value being the pid of that test process.

Should there be a pid ref used here instead of self()?

Ah, good catches folks! The text that @adamu pointed out is plain wrong, yep. The clarification below (that @cs44 highlighted) explains what’s going on in the right way. I fixed the first chunk of text and the example code. :slight_smile: Thanks for reporting! :heart_decoration: