Erlang Meck:如何只模拟特定的函数子句?

时间:2017-07-25 06:45:44

标签: erlang meck

给一个带有多个子句的函数,我想只模拟一个特定的情况,并且对于每一个否则会导致'function_clause'错误的其他输入,我想让它由原始函数处理。它几乎就像是erlang meck中的选择性passthrough。

1 个答案:

答案 0 :(得分:7)

您需要使用meck:passthrough/1: 我用这样的函数创建了一个模块:

-module(demo).
-export([original/1]).

original(1) -> one;
original(2) -> two;
original(3) -> three.

然后在控制台上......

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

希望这会有所帮助:)