Mocking.jl icon indicating copy to clipboard operation
Mocking.jl copied to clipboard

Bug: @patch does not work when Mocking is not in scope

Open lklein-btig opened this issue 2 years ago • 1 comments

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.

lklein-btig avatar Feb 23 '23 13:02 lklein-btig

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.

rofinn avatar Mar 09 '23 21:03 rofinn

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

omus avatar Jul 15 '24 15:07 omus