Coder Social home page Coder Social logo

Comments (3)

krzysztof-jusiak avatar krzysztof-jusiak commented on August 28, 2024

Thanks @paulreimer for the questions

  • Can we still use the automatic mock generation?

    • Unfortunatelly not out of the box as automatic mock injection requres GMocks
    • You could still use it if you provide some sort of traits for the mocks, though
      template<class> struct Mock;
      
      template<>
      struct Mock<MockPacketStream> {
       MOCK_CONST_METHOD1(GetPacket, const Packet*(size_t packet_number));
       MOCK_CONST_METHOD0(NumberOfPackets, size_t());
      };

    And this automatic mocks injection when resolve a type T

    return Mock<T>();
  • Can we still use GUnit.GMake? (since we are using derived class template args, not static_cast<iface&>)

  • Sort of, as described above
  • Can we still use Boost.DI? (I know, a question for a different repo)
  • Yes, no issues here

     example::example(ConcretePacketStream &); // no problems here, wiring not even needed

    With templates no issues too

     template<class T = class Stream>
     example::example(T stream);
    di::bind<class Stream>.to<ConcretePacketStream>()

One thing, which might be useful when using templates is to avoid writing macros, though.
That can be done by defining an interface instead and using GMock, automatic mock injection and DI without loosing performance at all as it will be injected via templates.

struct IStreamJustForTesting {
  virtual ~IStreamJustForTesting () = default;
  virtual void AppendPacket(Packet* new_packet) = 0;
  virtual const Packet* GetPacket(size_t packet_number) const =0;
  virtual size_t NumberOfPackets() const = 0;
};
di::bind<class Stream>.to<GMock<IStreamJustForTesting>()

from gunit.

paulreimer avatar paulreimer commented on August 28, 2024

OK, sounds good. I'm still trying to understand the first "automatic mock injection" part. Actually my question was more about the "automatic mock generation" part (e.g. avoiding macros), looks like it is possible as in the final example. So, at least we can define the interface in C++ (even if the concrete class doesn't inherit it) and avoid the MOCK_* macros (yay!).

Also just to check if I understand correctly, but in the final example then any mocks generated for IStreamJustForTesting will have dynamic dispatch (and use static_cast), but this only applies to the mocks and the production implementations do not need that interface? (and this is the price we pay -- at test time -- for avoiding the macros)

from gunit.

krzysztof-jusiak avatar krzysztof-jusiak commented on August 28, 2024

Right, Macros might be avoided with interfaces and GMock.

Yes, exactly. No cost in production but dynamic dispatch in testing.

from gunit.

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.