我使用Google Test和Google Mock进行单元测试,并且需要检查对象的调用,该对象放在std :: vector中。像这样:
TEST(FooTest, someTest)
{
// Given
CSomeClass someObject;
StrictMock<MockFoo> strictFoo;
std::vector<Foo> fooContainer;
fooContainer.push_back(strictFoo);
// Expected
EXPECT_CALL(strictFoo, doSomething()); // can't do this because there is a copy of strictFoo in vector
// When
someObject.do(fooContainer);
}
我非常感谢有关完成此任务的任何提示。
答案 0 :(得分:1)
我想我会选择更像这样的东西:
TEST(FooTest, someTest)
{
// Given
CSomeClass someObject;
std::vector<std::unique_ptr<Foo>> fooContainer;
fooContainer.emplace_back(new MockFoo);
// Expected
auto mock_foo_ptr(static_cast<MockFoo*>(fooContainer.rbegin()->get()));
EXPECT_CALL(*mock_foo_ptr, doSomething());
// When
someObject.Do(fooContainer);
}