mrand01
mrand01

Reputation: 261

Mocking with Dart - How to filter logs when a callsTo argument

I'm wondering if it's possible to filter logs by properties of passed arguments to a specific function. To be more specific, here's where I'm starting:

_dispatcher.getLogs(callsTo("dispatchEvent", new isInstanceOf<PinEvent>()));

I'd like to further filter this by PinEvent.property = "something"

In pseudo-code, I guess it'd look like this:

_dispatcher.getLogs(callsTo("dispatchEvent", new isInstanceOf<PinEvent>("property":"something")));

Any ideas? I know I can loop through the entire log list, but that seems dirty, and I would think there'd be a better way.

Thanks :-)

Upvotes: 1

Views: 65

Answers (2)

mrand01
mrand01

Reputation: 261

While waiting for responses I whipped this up real quick. It's probably not perfect, but it does allow you to combine an isInstanceOf with this matcher using allOf. ex:

allOf(new isInstanceOf<MyThing>(), new ContainsProperty("property", "propertyValue"));

And here's the Matcher:

class ContainsProperty extends Matcher {
  final String _name;
  final dynamic _value;

  const ContainsProperty(String name, dynamic value):this._name = name, this._value = value;

  bool matches(obj, Map matchState) {
    try {
      InstanceMirror objMirror = reflect(obj);
      InstanceMirror propMirror = objMirror.getField(new Symbol(_name));
      return propMirror.reflectee == _value;
    } catch (e) {
      return false;
    }
  }

  Description describe(Description description) {
    description.add("contains property \"${_name}\" with value \"${_value}\"");
  }
}

Upvotes: 1

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657348

You can write your own matcher and use it instead of isInstanceOf or derive from isInstanceOf and extend this matcher with the missing functionality.

I don't know of a matcher that does this out of the box (but I must admit that I didn't yet work very much with them).

Upvotes: 1

Related Questions