Skip to content

Instantly share code, notes, and snippets.

@leviwilson
Created February 17, 2011 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leviwilson/832076 to your computer and use it in GitHub Desktop.
Save leviwilson/832076 to your computer and use it in GitHub Desktop.
CppUTest - expectNCalls fails with ignoreOtherParameters
#include <iostream>
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTestExt/MockSupport.h>
using namespace std;
class Foo
{
public:
virtual void MyFunction(int something)
{
cout << "TestFunction in real Foo." << endl;
}
};
class Bar
{
public:
Bar(Foo* pFoo)
{
m_pFoo = pFoo;
}
void CallFooNTimes(unsigned int n)
{
for(auto i = n; i > 0; i--)
{
m_pFoo->MyFunction(i);
}
}
private:
Foo* m_pFoo;
};
class FooMock : public Foo
{
public:
void MyFunction(int something)
{
mock("Foo")
.actualCall("MyFunction")
.withParameter("something", something);
}
};
TEST_GROUP(FooTest)
{
void teardown()
{
mock().clear();
}
};
TEST(FooTest, TestDeficientNCallsToTestFunction_Fails)
{
FooMock fooMock;
Bar bar(&fooMock);
/**
* The issue seems to only be when you are expecting N calls to a function, but don't care
* about the parameters that are passed to it. If I don't add the .ignoreOtherParameters,
* the test will fail when is deficient.
**/
mock("Foo")
.expectNCalls(5, "MyFunction")
.ignoreOtherParameters();
bar.CallFooNTimes(4); // not enough
mock("Foo").checkExpectations();
// the only way to make this fail
CHECK_EQUAL(false, mock("Foo").expectedCallsLeft());
}
TEST(FooTest, TestSaturatedNCallsToTestFunction_Works)
{
FooMock fooMock;
Bar bar(&fooMock);
mock("Foo")
.expectNCalls(5, "MyFunction")
.ignoreOtherParameters();
bar.CallFooNTimes(6); // too many
mock("Foo").checkExpectations();
}
TEST(FooTest, TestJustEnoughNCallsToTestFunction_Works)
{
FooMock fooMock;
Bar bar(&fooMock);
mock("Foo")
.expectNCalls(5, "MyFunction")
.ignoreOtherParameters();
bar.CallFooNTimes(5);
mock("Foo").checkExpectations();
}
int main(int argc, char* argv[])
{
return CommandLineTestRunner::RunAllTests(argc, argv);
}
@basvodde
Copy link

Excellent! Fixed on the trunk now. It was an amusing bug.

@leviwilson
Copy link
Author

Was is really just the 1-line (chart*)malloc in AllocationInCFile.c? What was going on?

@basvodde
Copy link

Uhm, no. It was a 1-line fix :) (or actually a 0-line fix). But it was one revision back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment