如何对Erlang函数进行单元测试?

时间:2016-09-14 01:56:45

标签: unit-testing erlang

get_ue_supported_srvcc([]) ->
    ?SRVCC_3GPP_NONE_SUPPORT;
get_ue_supported_srvcc([#sip_contactV{extensionsP = EP} | T]) ->
    case b2bLib:support_tags_to_value(EP) of
    ?SRVCC_3GPP_NONE_SUPPORT ->
        get_ue_supported_srvcc(T);
    Flag ->
        Flag
    end.

我想为这个功能创建一个单元测试, 这是我的单元测试用例:

get_ue_supported_srvcc_test() ->
    Contact =
    [#sip_contactV{extensionsP =
              [{"+sip.instance",
            {quoted_string,"<urn:gsma:imei:35502406-005233-0>"}},
               {"+g.3gpp.icsi-ref",
            {quoted_string,"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel"}},
               "+g.3gpp.mid-call",
               "+g.3gpp.srvcc-alerting",
               "+g.3gpp.ps2cs-srvcc-orig-pre-alerting",
               "video"]}],

    ?assertEqual(7, b2bAtcfLib:get_ue_supported_srvcc(Contact)).

但是当我运行它时,我收到了这个错误:

======================== EUnit ========================
module 'b2bAtcfLib'
  b2bAtcfLib_tests: get_ue_supported_srvcc_test (module 'b2bAtcfLib_tests')...*failed*
in function b2bLib:support_tags_to_value/1
  called as support_tags_to_value([{"+sip.instance",{quoted_string,"<urn:gsma:imei:35502406-005233-0>"}},
 {"+g.3gpp.icsi-ref",
  {quoted_string,"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel"}},
 "+g.3gpp.mid-call","+g.3gpp.srvcc-alerting",
 "+g.3gpp.ps2cs-srvcc-orig-pre-alerting","video"])
in call from b2bAtcfLib:get_ue_supported_srvcc/1 (src/b2bAtcfLib.erl, line 1735)
in call from b2bAtcfLib_tests:'-get_ue_supported_srvcc_test/0-fun-0-'/1 (test/unit/b2bAtcfLib_tests.erl, line 49)
in call from b2bAtcfLib_tests:get_ue_supported_srvcc_test/0
**error:undef
  output:<<"">>

  [done in 0.008 s]
=======================================================

错误表示b2bLib:support_tags_to_value/1undef

此函数的定义b2bLib:support_tags_to_value

support_tags_to_value(FieldStr) ->
    lists:sum([Val || {Tag, Val} <- ?TAGLIST, lists:member(Tag, FieldStr)]).

1 个答案:

答案 0 :(得分:3)

错误是:

**error:undef

这意味着测试正在调用未定义的函数。无法找到该模块,或者该模块没有定义具有该名称和arity的函数。

整个错误消息有点令人困惑。现在我们知道我们得到了一个未定义的&#34;函数&#34;错误,我们应该看看这一行:

in function b2bLib:support_tags_to_value/1

即使它说错误发生了&#34;在&#34;这个函数,这是未定义的函数。

因此,测试运行的方式是它没有找到b2bLib模块,或者该模块没有定义一个名为support_tags_to_value的函数,只需要一个参数。如果是前者,请将-pa path/to/ebin添加到Erlang命令行,以便将正确的目录添加到代码路径中。