Giter Site home page Giter Site logo

Comments (11)

eranpeer avatar eranpeer commented on June 1, 2024 1

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.

eranpeer avatar eranpeer commented on June 1, 2024

from fakeit.

helmesjo avatar helmesjo commented on June 1, 2024

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.

helmesjo avatar helmesjo commented on June 1, 2024

Look also here at "Recursive Fakes" (just like in say NSubstitute).

from fakeit.

mfontanini avatar mfontanini commented on June 1, 2024

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.

eranpeer avatar eranpeer commented on June 1, 2024

from fakeit.

eranpeer avatar eranpeer commented on June 1, 2024

from fakeit.

eranpeer avatar eranpeer commented on June 1, 2024

from fakeit.

mfontanini avatar mfontanini commented on June 1, 2024

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.

ericlemes avatar ericlemes commented on June 1, 2024

@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.

mfontanini avatar mfontanini commented on June 1, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.