Comments (3)
Hi @et-nik,
Unfortunately ::
symbol breaks variable name generation in C++ macro. So it's not easy to fix. You can use the following approach with using namespace foo
in anonymous namespace block:
namespace foo {
int bar(std::string a) {
return 2;
}
}
namespace {
using namespace foo;
MOCK_GLOBAL_FUNC1(bar, int(std::string));
TEST(config_parse_test, Success) {
EXPECT_GLOBAL_CALL(bar, bar("baz")).WillOnce(Return(1));
int a = bar("baz");
ASSERT_EQ(a, 1);
}
}
Another option is to provide a different macro that accepts namespace, for example:
MOCK_GLOBAL_FUNC1_NS(foo, bar, int(std::string));
PR is welcomed!
from gmock-global.
Hi Sergey,
In the above test case, if we have foo namespace in a library and when we want to mock foo bar global function , mocking does not work.
Lets consider a case here.
foo namespace is used in static library "lib1". There is a method ( method 1) in this lib1 that calls foo bar function.
Now to mock foo bar in method 1, i have done following.
- created a unit test project. Added lib1 as reference along with gmock.
Test code looks like this.
namespace
{
//using namespace foo; // removed // foo is in one of the cpp files of lib1, so not adding
// if we keep foo in header file and add here, it creates multiple definition issue.
MOCK_GLOBAL_FUNC1(bar, int(std::string));
TEST(config_parse_test, Success) {
EXPECT_GLOBAL_CALL(bar, bar("baz")).WillOnce(Return(1));
int a =lib1->method1(); // this internally calls bar("baz");
ASSERT_EQ(a, 1);
}
}
after doing this, i am getting original bar call not the mocked one
please let me know on this.
from gmock-global.
Hi @Chandra123539 ,
gmock-global
can't magically replace existing functions, it can only create new ones. So you have to create a mechanism for function replacement by yourself.
You can set linker options to exploit a multiple definition issue and use the definition provided by mocks instead of the real one. Or it can be conditional compilation. Or linker scripts. Or function pointers. Whatever works best for you. I hope you've got the idea.
from gmock-global.
Related Issues (6)
- "warning C4172: returning address of local variable or temporary" under MSVC 2017 HOT 1
- how to recover the funtion?
- Segmentation fault when executing tests on a function that has been mocked in another test HOT 5
- Can I tell the global mock what should it return? HOT 1
- Compatibility with GoogleTest v1.10 HOT 3
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 gmock-global.