title: Testing iterative code using mocks - does it make sense, how?
body: <p>I want to test an algorithm using mocks. The algorithm - in the current implementation - iterates over a container class in multiple passes and set()s and get()s values from it. The test's purpose is to verify the final result, stored in the container. The final value depends on values read and written in between the passes. e.g. the value of any element may change multiple times until the algorithm is finished and, most-likely, its value as result of iteration n will depend on its value after iteration n-1.</p>
  
  <p>I like the idea of mocks and I would love to use them in the scenario described above since it would allow me to verify erroneous behavior of the algorithm once it occurs, not only when the computation is finished. However, I'm not sure whether this would actually be a good idea because I would then have to tie the expectations for the mock real close to the current implementation (e.g, "expect get(element n) and return x, then set(element n, value x+1), another get(n) and return x+1, then expect set(n, x+2) etc."). </p>
  
  <p>Although allowing me to verify that intermediate values are as expected, I think such expectations would contradict the purpose of the test (verify that the algorithm computes the correct final value) and likely the test would fail if the implementation changes, regardless of the correctness of the final value.</p>
  
  <p>Now my question: am I missing something? Is there nevertheless a good way to use mocks in this scenario? or does it simply make no sense to use them here? How do others deal with this problem?</p>
  
  <p>Final remark: I'm talking about testing c++ code and using googlemock, if that makes any difference to your answer.</p>
  
  <p>p.s.: I checked google and articles here (especially <a href="https://stackoverflow.com/questions/6438727/mocking-iterative-behaviour">Mocking iterative behaviour</a> - only tackles the problem of increasing a return value), however, I didn't find anything close to my problem.</p>