Git Product home page Git Product logo

mocking.jl's Introduction

Mocking

CI codecov.io

Code Style: Blue ColPrac: Contributor Guide on Collaborative Practices for Community Packages

Allows Julia function calls to be temporarily overloaded for purpose of testing.

Contents

Usage

Suppose you wrote the function randdev (UNIX only). How would you go about writing tests for it?

function randdev(n::Integer)
    open("/dev/urandom") do fp
        reverse(read(fp, n))
    end
end

The non-deterministic behaviour of this function makes it hard to test but we can write some tests dealing with the deterministic properties of the function:

using Test
using ...: randdev

n = 10
result = randdev(n)
@test eltype(result) == UInt8
@test length(result) == n

How could we create a test that shows the output of the function is reversed? Mocking.jl provides the @mock macro which allows package developers to temporarily overload a specific calls in their package. In this example we will apply @mock to the open call in randdev:

using Mocking

function randdev(n::Integer)
    @mock open("/dev/urandom") do fp
        reverse(read(fp, n))
    end
end

With the call site being marked as "mockable" we can now write a testcase which allows us to demonstrate the reversing behaviour within the randdev function:

using Mocking
using Test
using ...: randdev

Mocking.activate()  # Need to call `activate` before executing `apply`

n = 10
result = randdev(n)
@test eltype(result) == UInt8
@test length(result) == n

# Produces a string with sequential UInt8 values from 1:n
data = unsafe_string(pointer(convert(Array{UInt8}, 1:n)))

# Generate a alternative method of `open` which call we wish to mock
patch = @patch open(fn::Function, f::AbstractString) = fn(IOBuffer(data))

# Apply the patch which will modify the behaviour for our test
apply(patch) do
    @test randdev(n) == convert(Array{UInt8}, n:-1:1)
end

# Outside of the scope of the patched environment `@mock` is essentially a no-op
@test randdev(n) != convert(Array{UInt8}, n:-1:1)

Gotchas

Remember to:

  • Use @mock at desired call sites
  • Run Mocking.activate() before executing any apply calls

Overhead

The @mock macro uses a conditional check of Mocking.activated() which only allows patches to be utilized only when Mocking has been activated. By default, Mocking starts as disabled which should result conditional being optimized away allowing for zero-overhead. Once activated via Mocking.activate() the Mocking.activated function will be re-defined, causing all methods dependent on @mock to be recompiled.

License

Mocking.jl is provided under the MIT "Expat" License.

mocking.jl's People

Contributors

omus avatar tpgillam avatar fchorney avatar oxinabox avatar ararslan avatar iamed2 avatar rofinn avatar devmotion avatar staticfloat avatar juliatagbot avatar mjram0s avatar nickrobinson251 avatar mattbrzezinski avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.