作为meck的新手,我已经准备了一个测试来展示各种特性。但是,我不明白为什么开发人员会调用meck:validate。下面是我的例子:
-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).我遗漏了什么?
-更新以反映Adam解释的可被抓住的案件
发布于 2017-04-07 14:21:54
您成功地处理了所有未被验证的情况(在10c5063中添加了更好的文档)。
验证可以检测到:
function_clause)调用函数时meck:exception/2),这仍然导致true从meck:validate/1返回验证无法检测到:
它无法检测到这些情况的原因是Meck是如何实现的。Meck用模拟和维护模拟的进程替换模块。麦可得到的一切都通过模拟模块。Meck不在调用方级别(即在模块或测试用例中)插入自己,因此它不知道您未能调用模块。上述测试用例中的所有故障一开始都不会到达模拟模块。
https://stackoverflow.com/questions/43279705
复制相似问题