- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个具有引用参数的模拟函数:
MOCK_METHOD(func, void(MyObj &obj);
我想将 obj 捕获到一个变量中,以便我可以在单元测试中调用它的方法。所以像这样:
MyObj *capturedObj;
EXPECT_CALL(foo, func(testing::_)).WillOnce(<capture arg into capturedObj>);
capturedObj->bar();
我如何实现这一点?
最佳答案
一种可能的方法是调用一个单独的测试函数来为您捕获参数。
这可以通过 Invoke() 操作来完成,如 documentation 中所述:
Invoke(f) Invoke f with the arguments passed to the mock function, where f can be a global/static function or a functor.
Invoke(object_pointer, &class::method) Invoke the method on the object with the arguments passed to the mock function.
在您的情况下,类似下面的代码应该可以工作:
// Globally, outside your test case
MyObj *capturedObj;
void captureObj(MyObj &obj) {
captured = &obj;
}
// [..]
// In your test case
EXPECT_CALL(foo, func(testing::_)).WillOnce(testing::Invoke(captureObj));
capturedObj->bar();
如果使用测试夹具,capturedObj
和captureObj
可以是类成员,需要更新语法如下:
EXPECT_CALL(foo, func(testing::_)).WillOnce(testing::Invoke(this, &TestFixtureClass::captureObj));
关于unit-testing - GMock : Capture a reference argument in a mocked function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157888/
我在我的项目中使用 gmock,我遇到了为模拟函数设置自定义引用变量的问题。 假设我有一个类如下: class XXXClient { public: void QueryXXX(const
我正在尝试将 googlemock 集成到我的测试中。我已经在 googletest 上成功构建并运行了测试,现在我也在尝试逐步将 gmock 功能添加到测试中,但是我遇到了一个我完全不明白的编译错误
我正在进行一些测试,以便重构一个旧的 C++ 项目。我正在尝试使用 gmock 匹配器 ElementsAreArray() 来匹配两个数组。 EXPECT_THAT(value_instance.v
我正在尝试为向量编写测试。 对于 STL 容器,我尝试过: EXPECT_THAT(float_vec1, ElementsAreArray(float_vec2)); 但是我需要插入一个边距。 有没
我有一个具有接受可变参数的方法的类: class MyClass { public: virtual void myprint(const char* format, ...) = 0; }; 我试图
我想做这样的事情: EXPECT_CALL(*mock, method(5)).WillOnce(Return(arg1 * 2)); 其中 arg1 应等于被调用方法的第一个参数。有没有一种方法可以
我正在尝试模拟IAudioMeterInformation Windows API(我只需要GetPeakValue()方法)。 我收到一个错误: Error C2259 'AudioMeterInf
我使用GTest遇到了无法理解的行为。 问题很容易。我在叫sut方法。该方法调用StrictMock对象方法。我对该方法调用没有任何期望。根据GTest规范,此类测试应因“无趣的模拟函数调用”而失败。
我想做这样的事情: EXPECT_CALL(*mock, method(5)).WillOnce(Return(arg1 * 2)); 其中 arg1 应等于被调用方法的第一个参数。有没有一种方法可以
我有一个类,成员数组类型为int // Class Defenition class Foo { int array[5]; // ... Other Memebers } 另一个
我有一个类似这样的测试: #include using namespace ::testing; class IMyInterface { public: virtual ~IMyInter
我正在为一个类编写 GMOCK 测试用例: class A{ .. void Text() .. }; 现在A类的一个成员方法中嵌入了一个B类类型的对象,也引用了静态成员方法: void A::Tex
当我尝试像下面那样实现 GMOCK 方法时出现编译错误,但是当我删除它时代码和 MOCK 类工作正常: MOCK_METHOD2(myfunc, void (std::shared_ptr, std:
示例代码: struct MyFixture: public ::testing::Test { }; template struct MyFixtureWithParam: public MyFi
在 gmock 中是否有匹配类型而不是值的方法?该类是这样的: struct Blob { template bool is(); // if blob holds data of type
我刚开始用 gtest 和 gmock 做测试,然后我被一个问题阻止了。问题是这样的, 首先,我有一个这样的类, class Foo { public: std::vector& GetVec
我有一个测试对象,我想在真实对象中不需要的方法上调用 EXPECT_CALL,是否可以模拟这种未定义的新方法? struct MockObject { MOCK_METHOD2(onRecv,
我有以下模拟方法: MOCK_METHOD1(send, void(const std::vector& data)); 如何检查是否使用特定参数调用该方法,例如 std::vector vec{1,
我看到了一些关于 gmock 的示例代码, #include "gmock/gmock.h" #include "gtest/gtest.h" #include #include class Mo
这个问题在这里已经有了答案: Possible memory leak without a virtual destructor? (3 个答案) 关闭 5 年前。 代码 class A { pu
我是一名优秀的程序员,十分优秀!