I use Mockito for writing tests on Flutter. I have a mocked class and method with arguments that are functions - Function() and this method returns StreamSubscription. I need to pass these arguments to the call of listen() function but can't find a way to do it. (See example)
Could somebody help me, please?
I tried to pass them with argThat(anyNamed('nameOfArgument') like in the example, but tests trows error - The "anyNamed" argument matcher is used outside of method stubbing (via when)
class MockPhotoLibraryService extends Mock implements PhotoLibraryService {}
PhotoLibraryService setupMockPhotoLibraryService() {
final photoLibraryService = MockPhotoLibraryService();
when(
photoLibraryService.getPhotosForPeriod(
onData: anyNamed('onData'),
onDone: anyNamed('onDone'),
onError: anyNamed('onError')),
).thenAnswer((_) => Stream<Photo>.fromFuture(
Future<Photo>.delayed(Duration(milliseconds: 50), () => Photo()))
.listen(argThat(anyNamed('onData')), //need to pass argument named onData
onDone: argThat(anyNamed('onDone')), //need to pass argument named onDone
onError: argThat(anyNamed('onError')), //need to pass argument named onError
cancelOnError: true));
return photoLibraryService;
}
I need these arguments functions to be called by the Future for the correct work of my testable widget.
You can get access to the original call parameters through Invocation object. It is passed as a parameter to the thenAnswer callback function.
when(photoLibraryService.getPhotosForPeriod(
onData: anyNamed('onData'),
onDone: anyNamed('onDone'),
onError: anyNamed('onError'),
)).thenAnswer((Invocation invocation) {
final namedArgs = invocation.namedArguments;
final onData = namedArgs[Symbol('onData')] as Function(Photo);
final onDone = namedArgs[Symbol('onDone')] as Function();
final onError = namedArgs[Symbol('onError')] as Function(dynamic);
return Stream<Photo>.fromFuture(
Future<Photo>.delayed(Duration(milliseconds: 50), () => Photo()),
).listen(onData, onDone: onDone, onError: onError, cancelOnError: true);
});
Related
I want to create a reusable function in Electron.js to handle Saving data irrespective of the model(e.g User, Employee, Product),so I passed Model as an argument, then call the specific Model during when the function is called.
but I get this error
Error: Expected handler to be a function, but found type 'object'
This is my code
const User = require( '../database/models/Users.js');
ipcMain.handle('user:create', saveData(User));
async function saveData(_, data,Model) {
try {
const user = await Model.insert(data);
return user;
} catch (e) {
console.log(e.message);
}
}
ipcMain.handle('user:create', saveData(User)); call function saveData(User) after app is started and it returns object. if you want to assign function to 'user:create' then without parameters it's ipcMain.handle('user:create', saveData); but with parameters it's.
ipcMain.handle('user:create', () => saveData(User));
is the same as
ipcMain.handle('user:create', function () {
return saveData(User)
});
I've got some Future code that looks like this:
return login().then((user) {
print("Logged in user ${user.name}");
return user;
}).catchError(this.displayError);
Where I'm trying to pass a function to the catchError(...) function because it's an error handler I'm reusing across a number of calls. I've tried using this:
Future<void> displayError(Error error) {
return showDialog(context: context, builder: (context) {
return AlertDialog(...);
});
}
But when I run the app I get this error:
Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as arguments, and return a a valid result: Closure: (Error) => Future<void> from Function 'displayError':.
If I change my catchError(...) to this:
.catchError((error) {
displayError(error);
})
Everything then works. My question is does anyone know why I've not been successful passing the displayError function to catchError instead of call it within a closure?
Change the signature of the handler – it must be an Object not an Error. There are no promises in Dart that thrown objects are of type Error. They could be anything.
Future<void> displayError(Object error) {
return showDialog(context: context, builder: (context) {
return AlertDialog(...);
});
}
I know about the extension feature in Dart, but how can I use it with functions?
Essentially, I am facing two problems:
What do I extend on (extension FancyFunction on ?)?
I would want to add a function like toAsync that makes the function return a Future of its usual result.
How would I implement calling?
I could create a callAsync member that executes this() or this.call(), but I would like to use regular calling syntax, i.e. just parentheses.
What do I extend on when extending functions?
Dart has a Function type. This can be extended on and you can pass type parameters if you want.
Here is an example from the changelog:
extension CurryFunction<R, S, T> on R Function(S, T) { ... }
Furthermore, you can extend any typedef of a function.
For adding the toAsync and callAsync functionality a generic return type R will do. Note that this will only extend functions without parameters as Function() takes no parameters:
extension FancyFunction<R> on R Function() {
Future<R> Function() toAsync() => () async => this();
}
Now, this could be used like this:
void syncHello() => print('Hello');
void main() {
final asyncHello = syncHello.toAsync();
asyncHello().then((_) => print(', World!'));
}
How do I implement calling?
Every class in Dart can implement the call method. You can either execute this method simply using parentheses or with .call().
Here is an example implementation:
extension FancyFunction<R> on R Function() {
Future<R> call() async => this();
}
Since every Function already implements call, the extension member cannot be called implicitly.
Instead, you will have to explicitly declare your function as a FancyFunction to be able to call it:
void syncHello() => print('Hello');
void main() {
FancyFunction(syncHello)()
.then((_) => print(', World!'));
}
Note that FancyFunction(syncHello)() is the same method call as FancyFunction(syncHello).call().
However, now we have two problems:
We have to explicitly declare our function to be a FancyFunction, which somewhat defeats the purpose of having an extension.
The call method will always return a Future as the regular call method cannot be accessed anymore when we declare a FancyFunction.
Instead, adding a method like callAsync seems like a better solution:
extension FancyFunction<R> on R Function() {
Future<R> callAsync() async => this();
}
Now, callAsync can be used implicitly again:
void syncHello() => print('Hello');
void main() {
syncHello.callAsync()
.then((_) => print(', World!'));
syncHello(); // regular call
}
I am using Dart2
What I want to do is to force developer to use callback that accepts no or at most 1 argument.
For example, having following method:
void doYourJob(void onComplete([result])){ //this is what I have tried, buts its wrong - requires ([arg])=> callback
.... do your job
onComplete(result);
}
I would like to be able to use that method in two ways eg:
doYourJob(()=>doStuff);
and
doYourJob((result)=>doMoreStuffWithResult(result));
Is it possible to do something like this?
No. What you are trying to do is not possible.
You want to have a function type which accepts functions taking either zero or one argument. That is, you want to be able to pass a unary function and a nullary function.
That is, a function type which is a supertype of both void Function() and void Function(Object).
That is not the same as a function type with an optional parameter. Such a function type requires that all arguments must be callable both with zero and one argument. You cannot pass a pure unary function to that, because that function cannot be called with zero arguments.
There is no function type which is a supertype of both void Function() and void Function(Object). The nearest supertype is Function, which accepts any function, not just unary and nullary ones. You can use that, but you lose the type checking.
You can do this by creating a typedef
typedef Callback = Null Function([String data]);
void doYourJob(Callback onComplete) {
onComplete('Data');
onComplete();
}
OR
You can pass the Function directly
void doYourJob(Null Function([String data]) onComplete) {
onComplete('Data');
onComplete();
}
You can call this method like this
void main() {
doYourJob(([String data]) {
print('DATA: $data');
});
}
I have very small app:
main() async
{
Stream stream = Stream.fromIterable([1,2,3,4]);
stream.listen(
(d) { print(d); },
onDone: () { print("all done"); }
);
}
I can't understand why I can't call onData in the same manner as onDone like:
onData: (d) { print(d); }
Docs
The onData parameter to listen is a positional parameter, and the onDone parameter is a named parameter. All parameters are one or the other, and it determines how you pass an argument for that parameter.
To pass an argument for a named parameter, you must use the name, so you write onDone: () { ... }.
To pass an argument for a positional parameter, you just write the value. You cannot use the name.
So, to call listen with both, you write:
stream.listen(
(data) { handleData(...); },
onDone: () { handleDone(...); }
);
which passes one positional argument and one named arugment with the name onDone.