gpt4 book ai didi

c++ - 谷歌模拟 : How to configure custom message to explain match failure

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:34 25 4
gpt4 key购买 nike

如果匹配失败,Google mock 会打印如下消息:

test.cpp:112: EXPECT_CALL(mock_obj, foo( MyMatcher( bar ) ))...
Expected arg #0: is equal to [1,2; 3,4]
Actual: { 1 }
Expected: to be called once
Actual: never called - unsatisfied and active

使用自定义匹配器 MyMatcher 我可以定义一个描述字符串,用于在匹配失败时生成失败消息。但它只定义了消息的 Expected arg #0 部分。有什么方法可以自定义 Actual 的打印方式吗?

在我的例子中,我不能为 bar 的类重载 operator<< 因为它已经被第三方代码重载了,不在我的控制之下(这个库定义了 bar 和 operator<< 。

最佳答案

因为我不能能够定义operator <<对于我的 class ,谷歌测试有选项定义PrintTo它的功能Teaching Google Test How to Print Your Values .谢谢RA链接。

A user can override this behavior for a class type Foo by defining an overload of PrintTo() in the namespace where Foo is defined. We give the user this option as sometimes defining a << operator for Foo is not desirable (e.g. the coding style may prevent doing it, or there is already a << operator but it doesn't do what the user wants).

void PrintTo(const Bar& value, ::std::ostream* os);

关于c++ - 谷歌模拟 : How to configure custom message to explain match failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15349116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com