lundi 8 août 2016

Adding expected calls in a loop

This is what I want to achieve. In my test fixture I want to call a helper functions with a parameter n to tell the test fixture how many initialization sequences should be expected. Some parameters used in the sequences are stored in three std::vector containers; fileDescriptor, handle, selectionObject.

What I have written is this:

void MyTest::init_Ok(uint32_t n)
{
  for (uint32_t i = 0; i < n; ++i)
  {
    fileDescriptor.push_back(i); // FDs starting at 0
    handle.push_back(reinterpret_cast<void*>(18 + i)); // handles starting at 18
    selectionObject.push_back(555 + i); // SOs starting at 555

    EXPECT_CALL(MyMockApi::getApi(), initialize(Pointee(nullptr), StrEq("InitString"), MyMatcher()))
      .WillOnce(DoAll(SetArgPointee<0>(handle[i]),
                      Return(INIT_OK)));
    EXPECT_CALL(MyMockApi::getApi(), selectionObjectGet(handle[i], Pointee(nullptr)))
      .WillOnce(DoAll(SetArgPointee<1>(selectionObject[i]),
                      Return(SELECTION_OK)));
    EXPECT_CALL(MyMockApi::getApi(), finalize(handle[i]))
      .WillOnce(Return(FINAL_OK));
  }
}

I know why it's not working. It is expected that all calls to initialize will be identical but yet I want to perform different actions (parameter depending on loop counter i) for the first, second, third, ..., nth call. The current implementation will only expect one call to initialize no matter parameter n. Is it possible to fix this and keep it a loop somehow, or do I have to add the actions with a WillOnce line for each i? This would mean i have to check n and add different number of WillOnce lines for different possible values of n, which i really want to avoid.

Aucun commentaire:

Enregistrer un commentaire