erlangmeck

Erlang Meck: How does one mock only a specific function clause?


Give a function with multiple clauses, I'd want to mock only a specific case and for every other input that would otherwise cause a 'function_clause' error, I'd want to have it handled by the original function. It's almost like a selective passthrough in erlang meck.


Solution

  • You need to use meck:passthrough/1: I created a module with a function like this:

    -module(demo).
    -export([original/1]).
    
    original(1) -> one;
    original(2) -> two;
    original(3) -> three.
    

    Then on the consoleā€¦

    1> meck:new(demo).
    ok
    2> meck:expect(demo, original,
    2>             fun (1) -> not_one
    2>               ; (Arg) -> meck:passthrough([Arg])
    2>             end).
    ok
    3> demo:original(1).
    not_one
    4> demo:original(2).
    two
    

    Hope this helps :)