1

我使用 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);
}

我将不胜感激有关完成此任务的任何提示。

4

1 回答 1

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);
}
于 2013-05-22T01:09:47.280 回答