What's the point of meck:validate?

亡梦爱人 提交于 2019-12-23 15:56:42

问题


As a newcomer to meck, I've been putting together a test that shows the various features. I cannot, however, understand why a developer might call meck:validate. Here's my example:

-module(meck_demo).

-include_lib("eunit/include/eunit.hrl").

validate_is_of_limited_use_test_() ->
    { foreach, fun setup_mock/0, fun cleanup_mock/1, 
      [fun validate_does_not_fail_if_a_function_is_not_called/0,
       fun validate_does_not_fail_if_a_function_is_called_with_wrong_arity/0,
       fun validate_does_not_fail_if_an_undefined_function_is_called/0,
       fun validate_does_fail_if_a_function_was_called_with_wrong_argument_types/0,
       fun validate_does_fail_if_expectation_throws_an_unexpected_exception/0 ]}.

validate_does_not_fail_if_a_function_is_not_called() ->
    meck:expect(womble, name, fun() -> "Wellington" end),   
    ?assert(meck:validate(womble)).

validate_does_not_fail_if_a_function_is_called_with_wrong_arity() ->
    meck:expect(womble, name, fun() -> "Madame Cholet" end),
    ?assertError(undef, womble:name(unexpected_arg)),
    ?assert(meck:validate(womble)).

validate_does_not_fail_if_an_undefined_function_is_called() ->
    ?assertError(undef, womble:fly()),
    ?assert(meck:validate(womble)).

validate_does_fail_if_a_function_was_called_with_wrong_argument_types() ->
    meck:expect(womble, jump, fun(Height) when Height < 1 -> 
                                ok
                              end),
    ?assertError(function_clause, womble:jump(999)),
    ?assertNot(meck:validate(womble)).

validate_does_fail_if_expectation_throws_an_unexpected_exception() ->
    meck:expect(womble, jump, fun(Height) -> 42 = Height end),
    ?assertError({badmatch, 999}, womble:jump(999)),
    ?assertNot(meck:validate(womble)).   

setup_mock() ->
    meck:new(womble, [non_strict]).

cleanup_mock(_SetupResult) ->
    meck:unload(womble).

What am I missing?

-- Updated to reflect the cases that Adam explains can be caught


回答1:


You managed to hit just about every case not covered by validate (added better documentation in 10c5063).

Validate can detect:

  • When a function was called with the wrong argument types (function_clause)
  • When an exception was thrown
  • When an exception was thrown and expected (via meck:exception/2), which still results in true being return from meck:validate/1

Validate cannot detect:

  • When you didn't call a function
  • When you called a function with the wrong number of arguments
  • If you called an undefined function

The reason it cannot detect these cases is because of how Meck is implemented. Meck replaces the module with a mock and a process that maintains the mock. Everything Meck gets goes through that mock module. Meck does not insert itself at the caller level (i.e. in your module or in your test case), so it cannot know that you failed to call a module. All of the failures in your test case above never reaches the mock module in the first place.



来源:https://stackoverflow.com/questions/43279705/whats-the-point-of-meckvalidate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!