我正在使用meck测试我的gen_server mymodule。特别是,我按照httpc提供的说明使用meck模拟这里。
下面是我从测试中提取的一些代码:
do_some_tests_() ->
{foreach,
fun start/0,
fun stop/1,
[fun do_some_stuff/1,
fun do_some_other_stuff/1
]}.
start() ->
{ok, _} = mymodule:start_link(),
meck:new(httpc),
_Pid = self().
stop(_) ->
meck:unload(httpc),
mymodule:stop().
do_some_stuff(Pid) ->
%% here i use meck
meck:expect(httpc, request,
fun(post, {_URL, _Header, ContentType, Body}, [], []) ->
Reply = "Data to send back"
Pid ! {ok, {{"", 200, ""}, [], Reply}}
end),
%% here i do the post request
mymodule:myfunction(Body),
receive
Any ->
[
?_assertMatch({ok, {{_, 200, _}, [], _}}, Any),
?_assert(meck:validate(httpc))
]
end.有了这段代码,我可以让测试运行,但仍然有两件事我无法理解:
1)在结果中,我得到了这样的结果:
mymodule_test:43: do_some_stuff...ok
mymodule_test:43: do_some_stuff...ok
mymodule_test:53: do_some_other_stuff...ok
mymodule_test:53: do_some_other_stuff...ok每一次测试只能得到一行,而不是两行吗?
2)我如何为每次考试添加一个口语描述?
发布于 2013-08-12 08:16:37
函数do_some_stuff(Pid)生成两个测试,因此它非常正常,检查和显示都是正常的。
但是,您可以向每个生成器添加一个名称/描述& test:
do_some_tests_() ->
{foreach,
fun start/0,
fun stop/1,
[{"Doing some stuff" , fun do_some_stuff/1},
{"Doing some other stuff" , fun do_some_other_stuff/1}
]}.
do_some_stuff(Pid) ->
%% [code]
[
{"Check 200" , ?_assertMatch({ok, {{_, 200, _}, [], _}}, Any)},
{"Check httpc" , ?_assert(meck:validate(httpc))}
]
end.这应该显示如下内容:
module 'MyModule'
Doing Some Stuff
module:57: do_some_stuff (Check 200)...ok
module:58: do_some_stuff (Check httpc)...ok用EUnit的话来说,这就是所谓的“title”:
标题 任何测试或测试集T都可以用标题注释,方法是将其包装在一对{ Title,T}中,其中Title是一个字符串。为了方便起见,通常使用元组表示的任何测试都可以简单地给出一个标题字符串作为第一个元素,即编写{“Title”、.},而不是像{"The Title“、{.}那样添加额外的元组包装器。
http://www.erlang.org/doc/apps/eunit/chapter.html#id61107
https://stackoverflow.com/questions/18164295
复制相似问题