Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c++ - Uninteresting mock function call bla() && Expected: to be called at least once bla()?

I've written a small test with a mocked class. When I run it, first I get the warning that an uninteresting mock function was called and then the test fails because the expectation is not met, which is that the mocked function is called at least once. The funny thing is that that function is called as I see that warning message above.

Do you have any ideas on this matter?

Thank you!

Edit: This is my code structure:

class Bla {

public:
    Bla();
    virtual ~Bla();

    virtual float myFunction();
}

class MockBla : public Bla {
    MockBla();
    ~MockBla();
    MOCKMETHOD0(myFunction, float());
}

class CallerClass {

public:
    CallerClass() { MockBla* myMock = new MockBla(); }
    virtual ~CallerClass();

    myCallingFunction() { myMock->myFunction(); }
}

class MyTestClass : public ::testing::Test {
//....
    TEST(myTest, testMyFunction) {
    MockBla mockBla;
    EXPECT_CALL(mockBla, myFunction()).Times(AtLeast(1));

    CallerClass* callerClass;
    callerClass = new CallerClass();

    callerClass->myCallingFunction();

    }
//....
}

Result:

[ RUN      ] MyTestClass.testMyFunction

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: myFunction()
          Returns: 0
Stack trace:
MyTestClass.cpp:99: Failure
Actual function call count doesn't match EXPECT_CALL(mockBla, myFunction())...
         Expected: to be called at least once
           Actual: never called - unsatisfied and active
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to set the expectations on the actual instance of your mocked class which will be called during the test's execution.

In your case, you're setting the expectations on the object mockBla which is only constructed then destructed at the end of the test - it is never used.

You'll either need to pass the mock object into the CallerClass to use, or allow the CallerClass to create the mock object as a member variable, but then allow the test access to that actual member (via e.g. a getter or allowing the test to be a friend of the CallerClass).

An example of passing the mocked object into the calling class would be something like:

#include <memory>
#include "gmock/gmock.h"

class Bla {
 public:
  virtual ~Bla() {}
  virtual float myFunction() = 0;
};

class MockBla : public Bla {
 public:
  MOCK_METHOD0(myFunction, float());
};

class CallerClass {
 public:
  explicit CallerClass(Bla* bla) : mBla(bla) {}
  void myCallingFunction() { mBla->myFunction(); }
 private:
  Bla* mBla;
};

TEST(myTest, testMyFunction) {
  std::shared_ptr<Bla> mockBla(new MockBla);
  EXPECT_CALL(*std::static_pointer_cast<MockBla>(mockBla),
              myFunction()).Times(testing::AtLeast(1));

  CallerClass callerClass(mockBla.get());
  callerClass.myCallingFunction();
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...