How to check the function call order

Given this code:

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

  // other methods
private:
  vector<Contained> m_contained;
}

I want to check that if I fill a vector with three values, my functions will be called in the correct order and quantity. For example, my test might look something like this:

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()

How do you recommend testing this? Are there any tools for this using the CppUnit or GoogleTest frameworks? Maybe some other unit test frameworks allow you to run such tests?

I understand that perhaps this is not possible without calling any debugging functions from these functions, but at least it can be done automatically in some test environment. I do not like to scan trace logs and check their correctness.

UPD: , , ( , , ).

+3
7

, , .

, , , . , , , .

, , , , , . , , .

, , , :

  • , Collaborator
  • ToBeTested
  • , ToBeTested Collborator
  • , , .

cpp, , , , .

+1
+1

, , .

, ... ?

, , . - , .

, , ( AddContained?).

+1

, , , , , .

+1

, , . , RhinoMocks # .

++-, , ++, , , .

0

http://msdn.microsoft.com/en-au/magazine/cc301356.aspx

This is a good article on Context Bound Objects. It contains some so advanced things, but if you are not lazy and really want to understand such things, it will be really useful.

At the end, you can write something like: [CallTracingAttribute ()] public class TraceMe: ContextBoundObject {...}

0
source

You can use the ACE debugging framework (or similar), and in your test, configure the debugging object to stream to a file. Then you just need to check the file.

0
source

Source: https://habr.com/ru/post/1697008/


All Articles