Is there any good way to check mock calls if it is copied in Google Mock Framework

StackOverflow https://stackoverflow.com/questions/16665189

  •  30-05-2022
  •  | 
  •  

Domanda

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.

È stato utile?

Soluzione

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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top