<p>Considering such code:</p>

<pre><code>class ToBeTested {
public:
  void doForEach() {
    for (vector&lt;Contained&gt;::iterator it = m_contained.begin(); it != m_contained.end(); it++) {
       doOnce(*it);
       doTwice(*it);
       doTwice(*it);
    }
  }
  void doOnce(Contained &amp; c) {
    // do something
  }
  void doTwice(Contained &amp; c) {
    // do something
  }

  // other methods
private:
  vector&lt;Contained&gt; m_contained;
}
</code></pre>

<p>I want to test that if I fill vector with 3 values my functions will be called in proper order and quantity. For example my test can look something like this:</p>

<pre><code>tobeTested.AddContained(one);
tobeTested.AddContained(two);
tobeTested.AddContained(three);

BEGIN_PROC_TEST()
SHOULD_BE_CALLED(doOnce, 1)
SHOULD_BE_CALLED(doTwice, 2)
SHOULD_BE_CALLED(doOnce, 1)
SHOULD_BE_CALLED(doTwice, 2)
SHOULD_BE_CALLED(doOnce, 1)
SHOULD_BE_CALLED(doTwice, 2)

tobeTested.doForEach()
END_PROC_TEST()
</code></pre>

<p>How do you recommend to test this? Are there any means to do this with CppUnit or GoogleTest frameworks? Maybe some other unit test framework allow to perform such tests?</p>

<p>I understand that probably this is impossible without calling any debug functions from these functions, but at least can it be done automatically in some test framework. I don't like to scan trace logs and check their correctness.</p>

<p><em>UPD</em>: I'm trying to check not only the <strong>state</strong> of an objects, but also the <strong>execution order</strong> to avoid performance issues on the earliest possible stage (and in general I want to know that my code is executed exactly as I expected).</p>

[{'answer_id': 78764, 'author': 'Doug R', 'author_id': 3271, 'author_profile': 'https://Stackoverflow.com/users/3271', 'pm_score': 0, 'selected': False, 'text': '<p>Some mocking frameworks allow you to set up ordered expectations, which lets you say exactly which function calls you expect in a certain order. For example, <a href="http://www.ayende.com/projects/rhino-mocks.aspx" rel="nofollow noreferrer">RhinoMocks</a> for C# allows this.</p>\n\n<p>I am not a C++ coder so I\'m not aware of what\'s available for C++, but that\'s one type of tool that might allow what you\'re trying to do.</p>\n'}, {'answer_id': 78775, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 1, 'selected': False, 'text': '<p>You could check out <a href="http://mockpp.sourceforge.net/" rel="nofollow noreferrer">mockpp</a>.</p>\n'}, {'answer_id': 78810, 'author': 'kbaribeau', 'author_id': 8085, 'author_profile': 'https://Stackoverflow.com/users/8085', 'pm_score': 2, 'selected': False, 'text': "<p>You should be able to use any good mocking framework to verify that calls to a collaborating object are done in a specific order.</p>\n\n<p>However, you don't generally test that one method makes some calls to other methods on the same class... why would you?</p>\n\n<p>Generally, when you're testing a class, you only care about testing its publicly visible state.  If you test\nanything else, your tests will prevent you from refactoring later.</p>\n\n<p>I could provide more help, but I don't think your example is consistent (Where is the implementation for the AddContained method?).</p>\n"}, {'answer_id': 78861, 'author': 'dimarzionist', 'author_id': 10778, 'author_profile': 'https://Stackoverflow.com/users/10778', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-au/magazine/cc301356.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-au/magazine/cc301356.aspx</a></p>\n\n<p>This is a good article about Context Bound Objects. It contains some so advanced stuff, but if you are not lazy and really want to understand this kind of things it will be really helpful.</p>\n\n<p>At the end you will be able to write something like:\n[CallTracingAttribute()]\npublic class TraceMe : ContextBoundObject\n{...}</p>\n'}, {'answer_id': 78901, 'author': 'metao', 'author_id': 11484, 'author_profile': 'https://Stackoverflow.com/users/11484', 'pm_score': 1, 'selected': False, 'text': '<p>Instead of trying to figure out how many functions were called, and in what order, find a set of inputs that can only produce an expected output if you call things in the right order.</p>\n'}, {'answer_id': 78902, 'author': 'metao', 'author_id': 11484, 'author_profile': 'https://Stackoverflow.com/users/11484', 'pm_score': 0, 'selected': False, 'text': '<p>You could use ACE (or similar) debug frameworks, and in your test, configure the debug object to stream to a file. Then you just need to check the file.</p>\n'}, {'answer_id': 79169, 'author': 'kbaribeau', 'author_id': 8085, 'author_profile': 'https://Stackoverflow.com/users/8085', 'pm_score': 2, 'selected': True, 'text': "<p>If you're interested in performance, I recommend that you write a test that measures performance.</p>\n\n<p>Check the current time, run the method you're concerned about, then check the time again.  Assert that the total time taken is less than some value.</p>\n\n<p>The problem with check that methods are called in a certain order is that your code is going to have to change, and you don't want to have to update your tests when that happens.  You should focus on testing the <em>actual requirement</em> instead of testing the implementation detail that meets that requirement.</p>\n\n<p>That said, if you really want to test that your methods are called in a certain order, you'll need to do the following:</p>\n\n<ol>\n<li>Move them to another class, call it Collaborator</li>\n<li>Add an instance of this other class to the ToBeTested class</li>\n<li>Use a mocking framework to set the instance variable on ToBeTested to be a mock of the Collborator class</li>\n<li>Call the method under test</li>\n<li>Use your mocking framework to assert that the methods were called on your mock in the correct order.</li>\n</ol>\n\n<p>I'm not a native cpp speaker so I can't comment on which mocking framework you should use, but I see some other commenters have added their suggestions on this front.</p>\n"}]
['https://Stackoverflow.com/questions/78723', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14535/']