Friday, April 22, 2011

Google Mock Sequence

We have had a problem in our software for some time now that has caused plenty of frustration and headaches. We have a method that when it is called it builds a pipe-delimited string of values by looking up different pieces of data that had been saved off earlier. The string that is returned needed to have the values in a specific order; the reason behind this is that this string was parsed by an .asp page that had no knowledge of the order of the values. Anytime a developer would change the order of the values, it would break the web application that used these values. We all agree that this is an awful way of doing things but we currently have technology constraints that will not let us change this.

We have tried to educate the developers not to add items in the middle by sending the information out in emails, mentioning this in team meetings, and even putting comments in the code. Without fail though, someone will be in a hurry and forget about this and the next time someone goes to test the web application, it will have been broken.

Google Mock has a way to group expectations into a sequence that will cause the test to fail if not used in the proper order.

using ::testing::InSequence;
{
  InSequence pipeDelimitedStringOrder;

  EXPECT_CALL(...)...;
  EXPECT_CALL(...)...;
  ...
  EXPECT_CALL(...)...;
}

Now if a developer goes to change this method, they will see that the tests fail and will (hopefully) add any new methods to the end of the list.

No comments:

Post a Comment