本文介绍了meck的意义是什么?验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为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() - >Wellingtonend),
?assert(meck:validate(womble))。

validate_does_not_fail_if_a_function_is_called_with_wrong_arity() - >
meck:expect(womble,name,fun() - >Madlet Choletend),
?assertError(undef,womble:name(unexpected_arg)),
?assert(meck :验证(旺布尔))。

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:当高度< 1 - >
ok
结束时,expect(womble,jump,fun(Height)),
?assertError(function_clause,womble:jump )),
?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(梅克:验证(旺布尔))。

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

cleanup_mock(_SetupResult) - >
meck:卸载(womble)。

我缺少什么?



- 更新以反映Adam解释可以被捕获的情况

解决方案

你设法打了每个案件都不被验证所涵盖(在中添加更好的文档)。



验证可以检测:




  • 当函数被调用时带有错误的参数类型( function_clause

  • 引发异常时

  • 抛出异常并预期(通过 meck:exception / 2 ),仍然导致 true 返回meck:validate / 1



验证无法检测:




  • 当你没有调用函数

  • 当你调用一个函数数不正确的函数

  • 如果您调用了未定义的函数



无法检测到这些情况的原因是因为实现了Mock 。 Meck用模拟和维护模拟的过程取代了模块。所有的Meck都可以通过这个模拟模块。 Meck不会将自身插入到调用者级别(即在您的模块或测试用例中),因此不能知道您未能调用模块。您的测试用例中的所有故障都不会首先到达模拟模块。


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

解决方案

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 Mock 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.

这篇关于meck的意义是什么?验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 08:38