本文介绍了嬉皮士期望与类实例的电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置将类实例或结构实例作为"with"部分中的参数之一的期望调用?该文档似乎没有显示如何执行此操作.我看到使用简单的参数,例如字符串和整数-但不是类实例.通常您是否只是将参数设置为"_"并完成该操作?

How does one set up an expect call with a class instance or structure instance as one of the parameters in the "with" part? The documentation doesn't seem to show how to do that. I see using simple arguments like strings and integers - but not class instances. Do you usually just set that parameter as "_" and be done with it?

推荐答案

Dascandy已经对其进行了解释.

Dascandy already explained it.

解决方案是提供您自己的比较器实现,例如对于上面的示例:

Solution is, to provide your own comparer implementation, e.g. for the example above:

inline bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
    if ((lhs.a == rhs.a) && (lhs.b == rhs.b))
    {
        return true;
    }
    return false;
}

这会使编译器关闭,并且测试按预期进行:

That makes the compiler to shut up and the test works as expected:

TEST(check_CanCompareStructArguments)
{
    MockRepository mocks;

    IStruct* is = mocks.Mock<IStruct>();
    MyStruct ms;
    ms.a = 5;
    ms.b = 7;

    mocks.ExpectCall(is, IStruct::A).With(ms);

    is->A(ms);
}

这篇关于嬉皮士期望与类实例的电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:46