0

I have the following scenario where I am invoking EXPECT_CALL to set the return value of the function Foo to true however I am getting the following error

Actual: never called - unsatisfied and active

One essential thing I have seen is passing an object to EXPECT_CALL which is the case here. As in classPtr is a valid pointer to the MockClass object.

// Class Under Test

class Feature
{
    [[maybe_unused]] virtual bool Foo(const std::string& digit, int number);
};

// Mock class

class MockClass : public Feature
{
  public:
  MOCK_METHOD(bool, Foo, (const std::string& digit, int number),
              (override));
};

// Test class

class UtClass : public Ut
{
   public:
   std::unique_ptr<MockClass> classPtr = std::make_unique<MockClass>());
}

TEST_F(UtClass, Test)
{
    EXPECT_CALL(*classPtr, Foo("A", 5))
    .WillOnce(Return(true));   // Actual: never called - unsatisfied and active
}
273K
  • 29,503
  • 10
  • 41
  • 64
xyf
  • 664
  • 1
  • 6
  • 16

1 Answers1

1
  1. You do not call Foo. "Never called" is an expected error.
  2. You should create and destroy MockClass, use local MockClass mock_class in the test case TEST_F(UtClass, Test), otherwise you will get errors about mock object leaks.

It is supposed to be something like this

class Feature {
  [[maybe_unused]] virtual bool Foo(const std::string& digit, int number);
};

class Consumer {
 public:
  bool Eat(Feature *f) {
    return f->Foo("A", 5);
  }
};

class MockClass : public Feature {
 public:
  MOCK_METHOD(bool, Foo, (const std::string& digit, int number), (override));
};

class UtClass : public ::testing::Test {
};

TEST_F(UtClass, Test) {
  MockClass mock;
  EXPECT_CALL(mock, Foo("A", 5)).WillOnce(Return(true));

  Consumer consumer;
  consumer.Eat(&mock);
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • 1. How should `Foo()` be invoked then? 2. Shouldn't unique_ptr take care of deallocation? It's not a raw pointer which needs to be manually deallocated from my understanding – xyf Aug 04 '22 at 01:06
  • 1. You can call `Foo()` directly in the test like `classPtr->Foo("A", 5);` but this won't test much. Usually this is done together with dependency injection, i.e. when you pass `classPtr` to some other class that is under test and set expectation that this other class will do with your `classPtr`. See https://stackoverflow.com/questions/40508033/dependency-injection-with-unique-ptr-to-mock. 2. The code you provided is fine, no need to deallocate the unique_ptr. `UtClass` ctor creates the `unique_ptr` (before `TEST_F` is called) and dtor will deallocate it (after `TEST_F` is finished). – Quarra Aug 04 '22 at 11:44
  • *Shouldn't unique_ptr take care of deallocation?* You should not use the smart pointer at all. `MockClass mock_class;`, the local variable in `TEST_F`. "Mock object leak" is not a memory leak, it's a possible runtime error in Google mock post conditions. – 273K Aug 04 '22 at 14:46
  • are you implying to use `EXPECT_CALL(*classPtr, classPtr->Foo("A", 5))`? that won't work @Quarra – xyf Aug 04 '22 at 16:19
  • thanks for the snippet. you wouldn't use a `unique_ptr` per se? why not? – xyf Aug 05 '22 at 16:53