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

Any way to keep the original source clean?

Open tk3369 opened this issue 6 years ago • 7 comments

Let's say I have a function that looks like this:

function do_something_useful(a, b, c)
    d = @mock(first_part(a, b))
    if @mock(second_part(c, d))
        @mock(third_part())
    else
        @mock(forth_part())
    end
end

As you can see, I have to put @mock everywhere if I want to be able to patch all of them. The trouble is that the code does not look clean anymore. Would it be possible to do something like:

@mockfuns first_part second_part third_part forth_path 

@mock function do_something_useful(a, b, c)
    d = first_part(a, b)
    if second_part(c, d)
        third_part()
    else
        forth_part()
    end
end

tk3369 avatar Oct 20 '19 05:10 tk3369

An approach like the would be possible but would make the macro processing more complicated than it is now. Something that is being looking into is using Cassette.jl to avoid having to use the @mock macro at all. Unfortunately that approach can have some performance impacts when running tests.

An alternative that may work for you would be to just mock do_something_useful calls instead of mocking each function within.

omus avatar Oct 20 '19 13:10 omus

Is the Cassette version still being worked on?

How bad is the performance impact? Depending on the use case, performance may or may not be a concern....

tk3369 avatar Oct 20 '19 15:10 tk3369

Should probably make I new Cassette based version. After the 0.7 changes

oxinabox avatar Oct 20 '19 16:10 oxinabox

SimpleMock.jl is based on Cassettevand already works @tk3369

oxinabox avatar Oct 20 '19 17:10 oxinabox

Maybe another option is to make the IDE hide the @mock(...) part of the code 😉

tk3369 avatar Oct 20 '19 18:10 tk3369

We did the experiment with Cassette some time ago at this point but unfortunately there was too much of a performance reduction in comparison to using the @mock macro: https://github.com/JuliaTesting/Mocking.jl/issues/53#issuecomment-572204715

omus avatar Jul 15 '24 15:07 omus

Doing it with CassetteOverlays.jl could be a good solution. It shouldn't have any of the issues Cassette has.

oxinabox avatar Jul 16 '24 12:07 oxinabox