Comments (4)
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.
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.
🙂 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.
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)
- Confusing failure with nested mocks HOT 6
- Calling `when` consecutively doesn't stub mocks HOT 2
- Not compatible with freezed 2.4.2 HOT 5
- mocktail could offer string diffs on call failures? HOT 2
- mockNetworkImages should provide a way to test the errorBuilder from Image.network
- Should when() calls have a better error message for not having a following thenAnswer/thenReturn call?
- [Question] Verify constructor HOT 2
- Matcher is not working properly is specific case due to Object? HOT 2
- How to Mock multiple calls? HOT 1
- How to Mock a State? HOT 1
- while giving header or body to http. mock type 'Null' is not a subtype of type 'Future<Response>' this error coming HOT 3
- Repository.updateItem() Function Throws Null Subtype Exception in Bloc Test Event HOT 1
- captureAny list order differs in release mode HOT 3
- type 'Null' is not a subtype of type 'Entity'
- Mock or skip void method and global method HOT 1
- Mock with default stub implementation feature HOT 3
- Verify of objects passed in isolates don't work HOT 1
- Used on a non-mocktail object HOT 1
- mocktail_image_network: HTTP request failed, statusCode: 400 under certain conditions HOT 1
- mocktail gives confusing errors when there is an incomplete mock HOT 1
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 mocktail.