I suspect that each invocation has a known number of parameters. In the setup phase of the specific test case you know what you expect, so maybe you don;t need a loop. If you are trying to refactor out duplication, you could have helper functions that use 1, 2, 3, ... of the optional parameters.
If you really need to do it though, you can probably setup the test with CppUMock as is. Notice the return type of expectOneCall, and expectNCalls:
class MockSupport
{
public:
MockSupport();
virtual ~MockSupport();
virtual void strictOrder();
virtual MockFunctionCall& expectOneCall(const SimpleString& functionName);
virtual MockFunctionCall& expectNCalls(int amount, const SimpleString& functionName);
.....
It returns a reference to a MockFunctionCall. That's how the mock calls can be strung together. Though I have not done this, and am not sure if it is a good idea...
MockFunctionCall& mockCall = mock().expectOneCall( "Trace_Print" );
now you can loop like this:
loop:
{
mockCall.addParameter( "myArg1" );
}
close with this:
mockCall.ignoreOtherParameters()
mockCall.andReturnValue( (int) status );
Maybe Bas will comment
James
The mock_c() works the same as in C++. I've actually never seen it before in C, but it worked pretty well :)
Each call returns a struct with function pointers, so therefore you call a function pointer each time, which allows you to chain method calls in C.
Its kinda weird for a C programmer, but it worked pretty well (under the hood, it just converts to C++)
Bas
James already gave an option which ought to work ;)
Personally, I always use the vsnprintf way that you described as it will make the tests look a lot nicer. From the unit test perspective, I don't really care how it formats the message, but I'd like to test that the message is what it is :)
Why do you feel that would make the tests brittle?
Bas
Sorry for the late reply!
I tend to test the formatted string precisely for the reason you mention. When I chose to have a formatted string, I'd like to actually let it be formatted accurately. So, I'll have *one* test that checks the printout using the resulting format. If the printout isn't important, I wouldn't use it in more than one test, so that I don't have duplication and becomes maintainable. That way, if someone changes the format he'd need to change one test, which I think is actually good :)
I addition to that, using the formatted string also makes your test a lot more readable.
Bas
Wow, you even want to check the types :) I'm impressed :P
The code looks pretty good. Did it eventually did what you wanted?
Bas
> mea culpa.
>
> I found I did something terribly evil.
Well, terribly evil :) We get a bug report about that every now and then, it is fairly common.
You can actually do this automatically by installing the "MockSupportPlugin". You do this something like this (in main):
MockSupportPlugin mockSupport;
TestRegistry::getCurrentRegistry()->installPlugin(&mockSupport);
This will automatically do the checkExepctations and clean at the end of every test.
Bas
ps. design-wise you have to do this as the CppUTest base doesn't know anything about the mocking framework, so you'll need to tell it to clean up the mocks after teardown :)