Mocking.jl
Mocking.jl copied to clipboard
Bug: @patch does not work when Mocking is not in scope
julia> using Mocking: @patch
julia> f() = 5
f (generic function with 1 method)
julia> f_patch = @patch f() = 6
ERROR: UndefVarError: Mocking not defined
Stacktrace:
[1] top-level scope
@ REPL[3]:1
The @patch macro expects Mocking to be in scope, which does not need to be the case. The Blue style guide recommends putting things into scope as is done in the code snippet above for example.
Hmm, that's tricky. Since @patch is just generating code that uses the Mocking namespace it is in scope as far as Julia is concern, so the correct way to write that according to the Blue style guide is:
using Mocking: Mocking, @patch
...
Outside of getting rid of the macro altogether I don't think there's a good solution.
This was fixed in https://github.com/JuliaTesting/Mocking.jl/pull/113 and included in v0.7.8
Example:
julia> using Mocking: @patch
julia> f() = 5
f (generic function with 1 method)
julia> f_patch = @patch f() = 6
Mocking.Patch{typeof(f)}(f, var"##f_patch#225")
julia> using Mocking: @mock, activate, apply
julia> activate()
julia> apply(f_patch) do
@mock f()
end
6