Mecking riakc_pb_socket:get/3

Shunichi Shinohara shino at basho.com
Mon Nov 16 21:40:40 EST 2015


Hi Michael,

Sorry for very late response.
I tried mecking the module in erl shell.
In list passing style for expected arguments, Pid in call should
be the same term you passed in expect call, unless function_clause
is thrown.

> Pid = self().
<0.43.0>
> meck:expect(riakc_pb_socket, get, [Pid, {<<"t">>, <<"b">>}, <<"k">>], {ok, dummy}).
> riakc_pb_socket:get(Pid, {<<"t">>, <<"b">>}, <<"k">>).
{ok,dummy}
> Pid2 = spawn(fun() -> receive forever -> ok end end).
<0.57.0>
> riakc_pb_socket:get(Pid2, {<<"t">>, <<"b">>}, <<"k">>).
** exception error: function_clause

If you do not want to match the first argument, fun style can be used.

9> meck:expect(riakc_pb_socket, get, fun(_Pid, {<<"t">>, <<"b">>},
<<"k">>) -> {ok, dummy} end).
10> riakc_pb_socket:get(Pid2, {<<"t">>, <<"b">>}, <<"k">>).
{ok,dummy}
11> riakc_pb_socket:get(not_actually_pid, {<<"t">>, <<"b">>}, <<"k">>).
{ok,dummy}

Thanks,
Shino

2015-11-03 0:22 GMT+09:00 Michael Martin <mmartin4242 at gmail.com>:
> Hi all,
>
> I'm trying to meck riakc_pb_socket:get/3 in my eunit tests, and consistently
> get a function_clause error.
>
> The meck:expect looks like:
>
> meck:expect(riakc_pb_socket, get, [Pid, ?RIAK_TYPE_AND_BUCKET, ?TestOid],
> {ok, ?TestRiakObject}),
>
> where Pid is a pid, the macro ?RIAK_TYPE_AND_BUCKET evaluates to
> {<<"buckettype">>, <<"bucketname">>},
> and ?TestOid evalutes to
> <<"809876fd89ac405680b7251c2e57faa30004524100486220">>).
>
> With the exception of the Pid, the other arguments, as well as the expected
> response, are taken from a live,
> running system, where the call to riakc_pb_socket:get/3 works as expected.
>
> Looking at the source for riakc_pb_socket:get/3, I see that the -spec looks
> like:
>
> -spec get(pid(), bucket(), key()) -> {ok, riakc_obj()} | {error, term()}.
>
> and the types bucket() and key() are defined as:
> -type bucket() :: binary(). %% A bucket name.
> -type key() :: binary(). %% A key name.
>
> In reality, when using bucket types, shouldn't the bucket() type be a tuple?
> At any rate, changing it
> to tuple() didn't help my case any.
>
> Can anyone show me an example of a working meck:expect for
> riakc_pb_socket:get/3?
>
> Thanks,
> Michael
>
> _______________________________________________
> riak-users mailing list
> riak-users at lists.basho.com
> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com




More information about the riak-users mailing list