Git Product home page Git Product logo

Comments (2)

rollbear avatar rollbear commented on August 14, 2024

You have not shown what A looks like above, so it's not obvious what's happening, but it looks to me like you're expecting the library to work outside the rules of the language.

Unless A has a virtual function Add(int, int), then the call a->Add(x,y) is going to call the function in A::Add, no your MockA.::Add.

To answer the questions you asked: Static member functions cannot be mocked. Non-virtual functions can be mocked, but its use is limited. If it's not virtual, then obviously virtual dispatch won't happen. But it can be useful if the type is deduced in a template.

(contrived example)

template <typename T>
void test_func_call(T& t)
{
    assert(t.func(1) > 0);
}

struct mock
{
    MAKE_MOCK1(func, int(int));
};

int main()
{
    mock m;
    REQUIRE_CALL(m, func(1)).RETURN(3);
    
    test_func(m);
}

from trompeloeil.

garinyang avatar garinyang commented on August 14, 2024

yes, the function Add is virtual funciton as following:
class A {
public:
virtual int Add(int a, int b) {
return a + b;
}
};

about the point "Non-virtual functions can be mocked, but its use is limited."
RE: in case that I want to test the logic impl of a member functionA , and functionA called functionB which provided by another class, so I want to mock functionB, so that I can test only the logic impl of funcA.
^_^ If I used in my old project, it's not a good way to modify those 'old code', also it's not permitted, I think.

from trompeloeil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.