Giter Club home page Giter Club logo

Comments (4)

richaux avatar richaux commented on June 9, 2024

hi, I don't think that when(() => a.method2()).thenReturn(() => a.method1()); does reproduce the original behaviour, as method2 doesn't return anything. I think I'd expect a compilation error!

tbh I'm not convinced that mimicking invocations like this adds much value (I appreciate you've crafted a simpler example, so the original purpose might be hidden)... why would anything using AMock care what it gets up to internally? Seems an odd thing to be testing on the face of it.
hth (and apols if I've just misunderstood 😎)

from mocktail.

WieFel avatar WieFel commented on June 9, 2024

Thanks for the answer. Let me maybe go a bit more into details about the use-case:

I have a class MQTTProvider which handles messaging via MQTT and offers the following methods (amongst others):

  sendValue(String n, dynamic v) {
    // does the actual sending of n and v over MQTT
  }

  sendInt(String n, int v) {
    // does some conversion on v, then sends by calling sendValue()
    sendValue(n, v);
  }

  sendDouble(String n, double v, {int? numPlaces}) {
    // does some conversion on v, then sends by calling sendValue()
    sendValue(n, v);
}

The MQTTProvider is used in many different widgets of my app (a library of ~50 different kinds of components that communicate over MQTT). Some components might send int values and thus use sendInt. Some others might use sendDouble. Others even directly use sendValue.

So, in my tests I am setting up a "general" mock app which covers all these cases. I am mocking the MQTTProvider like so:

class MockMqttProvider extends Mock implements MQTTProvider {}

Now, when writing the tests for every one of these ~50 components, I want to check whether they send the expected values via MQTT, i.e. if sendValue was called with the expected arguments.
But as the MQTTProvider is mocked, a call to sendInt of course doesn't delegate to sendValue by default. That's why I am trying to do something like:

when(() => mockMQTTProvider.sendInt(any(), any())).thenReturn(mockMQTTProvider.sendValue);

What I want to avoid in the tests is having to look up for every component which concrete method (sendInt, sendDouble, ...) it uses to write the tests. What matters in the end is just that sendValue gets called.

I hope it is more clear now. On one side, i want to mock the MQTTProvider to be able to check whether a function has ben called with the expected arguments. But on the other side i want to preserve some of its behaviour, which is the correct delegation of function calls...

What would be your suggestion to do in this case?

from mocktail.

richaux avatar richaux commented on June 9, 2024

🙂 thanks, that describes it nicely.

My own personal preference is not to test such delegation in the components! 😐
The reasoning is that if a particular component has a contract to send stuff via sendInt, then it doesn't care that sendValue gets called, it's only responsible for calling sendInt.

What I want to avoid in the tests is having to look up for every component which concrete method (sendInt, sendDouble, ...) it uses to write the tests.

I think this is ok -- the components just use a vanilla MockMQTTProvider, no customisation required.

What matters in the end is just that sendValue gets called.

The responsibility for such correct delegation resides just in the MQTTProvider's own tests.

I appreciate this may not be the mocktail answer that you came here for! (I don't have any connection with the team, was just passing to thank them for their v1.0.0 efforts!). I hope the snippet below conveys what I'm on about.

It feels like you'd end up with reduced setup in your component tests yet added comfort around your Provider delegation; apols if they're no use at all. 🙂


Possible MQTTProvider implementations: one sets a sendType field you can interrogate; the other doesn't need the field but alters your API to return the type. [Not sure I like the String type but hope it makes sense.]

    String get sendType => _sendType;

  void sendValue(String n, dynamic v) {
    sendValue(n,v,"dyn");
  }

  void sendValue(String n, dynamic v, String sendType) {
    _sendType = sendType;
    // does the actual sending of n and v over MQTT
  }

  void sendInt(String n, int v) {
    // does some conversion on v, then sends by calling sendValue()
    sendValue(n, v, "int");
  }

  sendDouble(String n, double v, {int? numPlaces}) {
    // does some conversion on v, then sends by calling sendValue()
    sendValue(n, v, "dbl");
}

Alternate without field but setting return type.

  String sendValue(String n, dynamic v) {
    // does the actual sending of n and v over MQTT
    return "dyn";
  }

  String sendInt(String n, int v) {
    sendValue(n, v);
    return "int";
  }

  String sendDouble(String n, double v, {int? numPlaces}) {
    sendValue(n, v);
    return "dbl";
}

And tested like:

    final sut = MQTTProvider();
    test('correct route for int', () {
      sut.sendInt("xxx", 1);
      expect(sut.sendType, "int");
    });

or

    final sut = MQTTProvider();
    test('correct route for int', () {
      final result = sut.sendInt("xxx", 1);
      expect(result, "int");
    });

from mocktail.

felangel avatar felangel commented on June 9, 2024

Closing for now since it's been a while but if this is still an issue feel free to leave a comment and I'm happy to continue the conversation 👍

from mocktail.

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.