Patching module specific functions
Closed this issue · 2 comments
omus commented
Noticed when looking into #14. Mocking doesn't work if the user wants to mock a function from a specific module, Requests.get
vs get
.
using Mocking
Mocking.enable()
using Base.Test
import Requests
import HttpCommon
function foo(status::Int64)
Requests.statuscode(@mock Requests.get("http://httpbin.org/status/$status"))
end
patch = @patch Requests.get(x::String) = HttpCommon.Response(200, Dict{AbstractString, AbstractString}(), UInt8[])
apply(patch) do
@test foo(200) == 200
end
Additionally a user may accidently do the following and get get a "no method matching" exception for ismocked
.
using Mocking
Mocking.enable()
using Base.Test
import Requests
import Requests: get
import HttpCommon
function foo(status::Int64)
Requests.statuscode(@mock Requests.get("http://httpbin.org/status/$status"))
end
# Changed from Requests.get to just get
patch = @patch get(x::String) = HttpCommon.Response(200, Dict{AbstractString, AbstractString}(), UInt8[])
apply(patch) do
@test foo(200) == 200
end
oxinabox commented
This is pretty annoying.
I can take a crack at it at some point.
I think an unobtrusive change would be to name mangle
Foo.bar
into Foo_MOCKDOT_bar
and create a local function of that name.
(Maybe use a UUID instead of MOCKDOT
to reduce chance of clash -- at the cost of worse stack traces)
But other options exist.
Maybe something value rather than name based.