Question

I use Google Test and Google Mock for making unit tests and there is a need to check calls of object, which was placed into a std::vector. Like this:

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);
}

I'd greatly appreciate any tips on accomplishing this.

Was it helpful?

Solution

I guess I'd go for something more like this:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top