为什么 Eunit 坚持我的函数返回 {ok, value} 而实际上不是?

Why is Eunit insisting my function is returning {ok, value} when it's not?

我正在做一些非常简单的事情:在不使用 BIF 的情况下在 Erlang 中反转列表。

这是我的尝试:

%% -----------------------------------------------------------------------------
%% Reverses the specified list.
%% reverse(List) where:
%%   * List:list() is the list to be reversed.
%% Returns: A new List with the order of its elements reversed.
%% -----------------------------------------------------------------------------
reverse(List) ->
  reverse2(List, []).

%% -----------------------------------------------------------------------------
%% If there are no more elements to move, simply return the List
%% -----------------------------------------------------------------------------
reverse2([], List) -> 
  List;

%% -----------------------------------------------------------------------------
%% Taking the head of the List and placing it as the head of our new List
%% This will result in the first element becoming the last, the 2nd becoming
%% the second last, etc etc.
%% We repeat this until we arrive at the base case above
%% -----------------------------------------------------------------------------
reverse2([H|T], List) ->
  reverse2(T, [H|List]).

现在我相信这是正确的,并且在 shell 上使用随机列表对其进行测试每次都会给出正确答案。这是我用来 运行 Eunit 测试的代码:

%% -----------------------------------------------------------------------------
%% Tests the reverse list functionality.
%% -----------------------------------------------------------------------------
reverse_test_() -> [
  {"Reverse empty list", ?_test(begin

    % Reverse empty list.
    ?assertEqual([], util:reverse([]))
  end)},
  {"Reverse non-empty list", ?_test(begin

    % Reverse non-empty list.
    ?assertEqual([5, 4, 3, 2, 1], util:reverse([1, 2, 3, 4, 5]))
  end)}
].

这就是 运行ning 测试告诉我的,特别是:

error:{assertEqual,[{module,util_test},
              {line,101},
              {expression,"util : reverse ( [ ] )"},
              {expected,[]},
              {value,{ok,[]}}]}
  output:<<"">>

这让我感到困惑,我的函数 reverse 从来没有 return 使用 ok 进行任何操作,在终端中测试它不会使用 ok 输出任何内容,但是另一方面,使用 EUnit 测试它似乎 return {ok, value}

实际上代码看起来不错。也许你在 util.erl 中有一些其他功能 reverse/1 正在尝试 return {ok, Value}。尝试在没有 util: 指示的情况下更新您的测试:

%% -----------------------------------------------------------------------------
%% Tests the reverse list functionality.
%% -----------------------------------------------------------------------------
reverse_test_() -> [
  {"Reverse empty list", ?_test(begin

    % Reverse empty list.
    ?assertEqual([], reverse([]))
  end)},
  {"Reverse non-empty list", ?_test(begin

    % Reverse non-empty list.
    ?assertEqual([5, 4, 3, 2, 1], reverse([1, 2, 3, 4, 5]))
  end)}
].