简体   繁体   中英

Reference mocked class

my code is structured like this

class B
{
   virtual void bar() { //something};
}

class A {

void foo(B& b) { b.bar();}

}

I wanted to create a gtest mock for this but I'm running into an issue...

class Btest : public B
{
  public:
    MOCK_METHOD(void, bar, (), (override));
};

TEST()
{
   A a;
   Btest b;
   a.foo(b); <--- no instance matches argument list
}

How do I implement a mock like this? If I upcast wouldn't it just call the non mocked version of that method?

There are several issues with your code. One way to fix it is to make the bar and foo methods public:

class B {
 public:
  virtual void bar() {
    // something
  }
};

class A {
 public:
  void foo(B& b) { b.bar(); }
};

class Btest : public B {
 public:
  MOCK_METHOD(void, bar, (), (override));
};

TEST(a, b) {
  A a;
  Btest b;
  a.foo(b);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM