erlang - What's the point of meck:validate? -
as newcomer meck, i've been putting test shows various features. cannot, however, understand why developer might call meck:validate. here's 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 missing?
-- updated reflect cases adam explains can caught
you managed hit every case not covered validate (added better documentation in 10c5063).
validate can detect:
- when function called wrong argument types (
function_clause
) - when exception thrown
- when exception thrown , expected (via
meck:exception/2
), still results intrue
being returnmeck:validate/1
validate cannot detect:
- when didn't call function
- when called function wrong number of arguments
- if called undefined function
the reason cannot detect these cases because of how mock implemented. meck replaces module mock , process maintains mock. meck gets goes through mock module. meck not insert @ caller level (i.e. in module or in test case), cannot know failed call module. of failures in test case above never reaches mock module in first place.
Comments
Post a Comment