Comments (1)
Hi @Cantafford ,
This is quite an old issue but still in "open" state and my answer might be useful for other users.
If you create a fake object (in your case FakeMyClass ) you don't want to call original implementation (from MyClass). This is the whole point about making fake objects.
If you execute your code, here is what GMock will print:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: retValue()
Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
So GMock is telling you, that value 0 will be returned (hence EXPECT_EQ is not satisfied).
I will repeat it, the whole point is to NOT call original implementation from MyClass (if you want to call original implementation, just use object of original class :) ). If you injected this fake object to another object, and you want to force it to return value you need (let's say 3) you should specify that with GMock commands (EXPECT_CALL). In practice it should look like this:
TEST(TestForMyClass, TestRetVal)
{
FakeMyClass obj3;
EXPECT_CALL(obj3, retValue()).WillOnce(Return(3));
EXPECT_EQ(obj3.retValue(), 3);
}
If you still believe, that you want to call implementation from class MyClass, you can always do something like that:
TEST(TestForMyClass, TestRetVal)
{
FakeMyClass obj3;
EXPECT_CALL(obj3, retValue()).WillOnce(Return(obj3.MyClass::retValue()));
EXPECT_EQ(obj3.retValue(), 3);
}
Finally, if you don't really care how many times FakeMyClass is called and you just want to return "3" whenever it happens, you can use NiceMock:
TEST(TestForMyClass, TestRetVal)
{
FakeMyClass obj3;
NiceMock<FakeMyClass> niceMockForFakeClass;
ON_CALL(niceMockForFakeClass, retValue()).WillByDefault(Return(3));
EXPECT_EQ(niceMockForFakeClass.retValue(), 3);
}
I hope that my explanation helped you.
I think that this issue should be marked as solved and closed unless @Cantafford has some additional questions.
Regards,
Ikeban
from googlemock.
Related Issues (20)
- CMake Error in CMakeLists.txt HOT 2
- Mocking class and callbacks funcitons in 'anonymous namespace'
- test.camke line 16 add_library path error
- Don't throw nested exception from dtor of Mock object
- Problems when running tests with mock objects on Open BSD
- Unwanted warning when compiling with Clang HOT 2
- Ability to deinitialize googlemock
- ubhf
- gmock_ex_test.cc fails if exceptions are disabled HOT 1
- This project is still the top hit for [googlemock]
- Leakable mock object isn't really leaked HOT 1
- Pointee with overloaded functions
- Matching std::reference_wrapper objects with RefMatcher yields SEH exceptions or undefined behavior
- Compilation error:
- Mock class generation by running the script
- Misleading documentation about regular expression matcher HOT 2
- redefinition of function IgnoredValue
- How do you know how many times EXPECT_CALL(). Times (3) has been called?
- Warning reported from clang tidy on Call to virtual function during destruction
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from googlemock.