Comments (11)
Yea, I'm aware there are solutions in java & c# (and also in Isolator++), I just can't find a way to implement this one. I'll be happy if someone will submit a Pull Request on this issue.
from fakeit.
from fakeit.
Yeah, the absence of reflection is something one notices quickly coming from a .Net-background! :) At the same time one keeps getting amazed by the "magic" possible with Macro-nesting though!
Reading through Isolator++ docs, however, I noticed them having this feature (returning default values). How they've implemented their mocks, and what they really do in the background to allow this I don't know (haven't even tried their free trial), but it works.
See here:
class MyPureClass
{
public:
virtual int GetResult() = 0;
}
// ...
MyPureClass* fakeMyClass = FAKE<MyPureClass>();
[..] This fakes all the methods on the MyClass class. Primitives return 0, and methods returning pointers, return pointers to fake objects.
This is pretty much similar to how mocking frameworks in other languages work.
from fakeit.
Look also here at "Recursive Fakes" (just like in say NSubstitute).
from fakeit.
Is the default behavior to throw now? It seems to crash for me. e.g. this code:
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <catch/fakeit.hpp>
#include <string>
class Foo {
public:
virtual ~Foo() = default;
virtual std::string bar() = 0;
};
TEST_CASE("test") {
fakeit::Mock<Foo> mocked;
//fakeit::Fake(Method(mocked, bar)); < this fixes it
mocked.get().bar();
}
Blows up. Valgrind output:
==15726== at 0x44B4F8: fakeit::VirtualTableBase::getCookie(int) (fakeit.hpp:5678)
==15726== by 0x48215D: fakeit::MockImpl<Foo<> >::getMockImpl(void*) (fakeit.hpp:8158)
==15726== by 0x47B128: fakeit::MockImpl<Foo<> >::unmocked() (fakeit.hpp:8169)
==15726== by 0x42BF9B: ____C_A_T_C_H____T_E_S_T____0() (test.cpp:15)
==15726== by 0x41B157: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:10331)
==15726== by 0x41A7FC: Catch::TestCase::invoke() const (catch.hpp:10232)
==15726== by 0x4151ED: Catch::RunContext::invokeActiveTestCase() (catch.hpp:9104)
==15726== by 0x414F01: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:9078)
==15726== by 0x413ABF: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:8861)
==15726== by 0x416710: Catch::(anonymous namespace)::runTests(std::shared_ptr<Catch::Config> const&) (catch.hpp:9394)
==15726== by 0x417A4C: Catch::Session::runInternal() (catch.hpp:9592)
==15726== by 0x4177DA: Catch::Session::run() (catch.hpp:9549)
==15726==
==15726== Invalid read of size 8
==15726== at 0x44B4F8: fakeit::VirtualTableBase::getCookie(int) (fakeit.hpp:5678)
==15726== by 0x48215D: fakeit::MockImpl<Foo<> >::getMockImpl(void*) (fakeit.hpp:8158)
==15726== by 0x47B128: fakeit::MockImpl<Foo<> >::unmocked() (fakeit.hpp:8169)
==15726== by 0x42BF9B: ____C_A_T_C_H____T_E_S_T____0() (test.cpp:15)
==15726== by 0x41B157: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:10331)
==15726== by 0x41A7FC: Catch::TestCase::invoke() const (catch.hpp:10232)
==15726== by 0x4151ED: Catch::RunContext::invokeActiveTestCase() (catch.hpp:9104)
==15726== by 0x414F01: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:9078)
==15726== by 0x413ABF: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:8861)
==15726== by 0x416710: Catch::(anonymous namespace)::runTests(std::shared_ptr<Catch::Config> const&) (catch.hpp:9394)
==15726== by 0x417A4C: Catch::Session::runInternal() (catch.hpp:9592)
==15726== by 0x4177DA: Catch::Session::run() (catch.hpp:9549)
==15726== Address 0xffffffffffffffe0 is not stack'd, malloc'd or (recently) free'd
from fakeit.
from fakeit.
from fakeit.
from fakeit.
FakeIt has some nice syntax but it seems to be extremely unstable. I understand that's because of how it works under the hood but I think I'll stick to some other mocking library where crashing is not common behavior under any compiler flags.
from fakeit.
@mfontanini, let me know if you find anything that suits your needs. I tried a lot of different. The most stable seems to be GMock but also has it's drawbacks.
I think with some small improvements in Fake It (I was thinking if would be possible to create some sort of self validation to point out unsupported compiler flags, via static assert or even self testing) to simplify these issues. That's why I've been going down this route of trying to improve Fake It instead of moving away. It is really hard to find a good, stable and supported mocking framework for C++.
For me after one fix, it is definitely working very well.
from fakeit.
@ericlemes I've been using trompeloeil which has been working great so far, although I have just started using it so it may be too soon to say. I really like how FakeIt looks like but the fact that it just blows up when anything goes wrong is really worrying. How do you know the compiler won't start doing something that breaks FakeIt even when optimization is disabled in the future? In fact:
@eranpeer, building with -O0
doesn't fix this. I'm using g++ 5.4 (default on Ubuntu Xenial) and it still blows up. I'm only passing -O0 -std=c++14
as command line arguments when building that code.
from fakeit.
Related Issues (20)
- Overloaded function in derived class hides base class function HOT 5
- compiled error with c++17 in VS2019 HOT 4
- SIGSEGV when when mock with shared_ptr HOT 16
- complied error when mock with unique_ptr HOT 3
- exception with unique_ptr HOT 13
- Can't mock override with ref-qualifiers? HOT 5
- Colliding MockingContext ids when used in multiple translation units because of __COUNTER__ HOT 7
- Can't stub method with vector of unique_ptr parameter HOT 5
- Clang 14.0.6 compile error HOT 7
- Undefined sanitizer issue HOT 4
- Seems latest version 2.3.1 is not available on conan yet HOT 3
- fakeit from conan center doesn't get header with integration (e.g. header with gtest integration) HOT 5
- Quickstart spy example does not work as expected HOT 2
- Cannot verify 0 times invocations using RepeatedSequence HOT 4
- There are deprecated warnings on OSX with C++20 and C++23 HOT 3
- FakeIt and CppUTest HOT 3
- Verify Using QString gives bad_alloc HOT 2
- Missing .CoReturn method HOT 1
- Not working with local classes? HOT 1
- Stubbing method returning the vector of unique_ptr's generates the compile error. 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 fakeit.