Skip to content

Class mocking example doesn't work #88

@mje-nz

Description

@mje-nz

The "Sample usage of class mocking" example on the wiki fails, with the error "Function called without expectation!".

Is it possible to mock some methods of a class and leave the rest untouched? If not, the wiki needs to updated.

Here's the code I used:

class CFoo {
public:
    bool Func1() {
        bool ret = false;
        if (Func2()) {
            //do something
            ret = true;
        }
        else {
            //do something else
        }
        return ret;
    }

    bool Func2() {
        return true;
    }

};

TEST_CASE("Hippomocks example", "[sanity]") {
    MockRepository mocks;
    CFoo* pFoo = mocks.Mock<CFoo>();
    mocks.ExpectCall(pFoo, CFoo::Func2).Return(true);
    REQUIRE(pFoo->Func1() == true);
    mocks.ExpectCall(pFoo, CFoo::Func2).Return(false);
    REQUIRE(pFoo->Func1() == false);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions