Stubbed function always returns null - flutter

Currently I'm following a tutorial of Resocoder (DDD Course). In this course there is an abstract class called i_auth_facade.dart, which contains a function getSignedInUser.
I have mocked the class and trying to get a valid option of user back from the getSignedInUser method, but the result is always null.
Also when I try to execute the authCheckRequested method of the AuthBloc, which then in the background invokes the getSignedInUser method, I get back null.
I must have some error with Mockito, as it seems the stub doesn't work.
Can anybody point me into the right direction why I don't get back the optionOf(User), but null instead?
import 'package:bloc_test/bloc_test.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:teach_mob_student/application/auth/auth_bloc.dart';
import 'package:teach_mob_student/domain/auth/i_auth_facade.dart';
import 'package:teach_mob_student/domain/auth/user.dart';
import 'package:teach_mob_student/domain/auth/value_objects.dart';
import 'package:teach_mob_student/domain/core/value_objects.dart';
class MockIAuthFacade extends Mock implements IAuthFacade {}
void main() {
final tValidUser = User(
id: UniqueId(),
name: StringSingleLine('Test User'),
emailAddress: EmailAddress('test#user.com'));
test('Should return valid option of user', () async {
// This is to check, if optionOf actually returns a user --> Yes it does
Option<User> tDirectResult = optionOf<User>(tValidUser);
// Here I stub the getSignedInUser method. If it is executed, I want to receive a valid user.
when(MockIAuthFacade()
.getSignedInUser()
).thenAnswer((_) async => Future.value(optionOf<User>(tValidUser)));
// Here I execute the method, and I would expect to get Future<Option<User>> back, but I only get back null.
final facade = MockIAuthFacade();
Option<User> tStubbedResult = await facade.getSignedInUser();
print(tDirectResult);
print(tStubbedResult);
});
// Here I do the same, but I execute the authCheckRequested method of the AuthBloc, which executes the
// getSignedInUser method. Also here I get back null as result.
blocTest('Should return valid option of user.',
build: () async {
when(MockIAuthFacade()
.getSignedInUser()
).thenAnswer((_) async => Future.value(optionOf<User>(tValidUser)));
return AuthBloc(MockIAuthFacade());
},
act: (bloc) async {
bloc.add(AuthEvent.authCheckRequested());
},
expect: [
]
);
}

Related

The return type 'Null' isn't a 'Future<Message>', as required by the closure's context

i am using Mockitou package to Test my MessageBloc and the logic of my Message application in Flutter but i have common issue in all my tests mentioned in the title (The return type 'Null' isn't a 'Future<Message>', as required by the closure's context.) i Comment in the code where the error is
import 'package:chat/chat.dart';
import 'package:flutter_newapp/src/blocs/message/message_bloc.dart';
import 'package:flutter_newapp/src/blocs/message/message_event.dart';
import 'package:flutter_newapp/src/blocs/message/message_state.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
class FakeMessageService extends Mock implements IMessageService {}
void main() {
late MessageBloc sut;
late IMessageService messageService;
User user;
setUp(() {
messageService = FakeMessageService();
user = User(
username: 'test', photoUrl: '', active: true, lastSeen: DateTime.now());
sut = MessageBloc(messageService);
});
tearDown(() => sut.close());
test('it should emit initial state before Subscription',
() => expect(sut.state, MessageInitial()));
test('should emit message sent state when message is sent', () {
final message = Message(
from: '1234',
to: '123',
timestamp: DateTime.now(),
contents: 'Hellllo');
when(messageService.send([message])).thenAnswer((_) async => null); // Here is the Error
sut.add(MessageEvent.onMessageSent([message]));
expectLater(sut.stream, emits(MessageState.sent(message)));
});
}
So if anyone have a Solution i will be So grateful
Do i have to Override the Methods of IMessageService or Something ? i think that this is the problem
I think messageService.send([message]) returns a type of Future<Message>, that's why you're getting this error.
You can modify that method and set the type to Future<Message?> or provide a dummy Future<Message> value in the test itself.
Because right now your method can't return null and you are returning null from the test that's why you're facing the issue.
Hope it helps.
This error is occurring because the closure you are passing to then fun is not returning the correct type. The closure is expected to return a Future, but it's returning null instead.
To resolve this error, you need to make sure that the closure you pass to then fun returns a Future that completes successfully. You can do this by returning a Future. value(null) instead of just null.
when(messageService.send([message])).thenAnswer((_) async => Future.value(null));

How to mock url_launcher package for flutter test?

I used url_launcher: ^6.1.0 in my flutter project.
I start to write tests for my widgets, but the part of widgets that used the url_launcher method to launch an URL, not worked properly when running the test.
One of the methods that I used inside my Widget is like below method:
Future<void> _onTapLink(String? href) async {
if (href == null) return;
// canLaunchUrl method never return anything when we are calling this function inside flutter test
if (await canLaunchUrl(Uri.parse(href))) {
await launchUrl(Uri.parse(href));
} else {
print('cannot launch url: $href');
}
}
canLaunchUrl method never returns anything when we are calling this function inside the flutter test.
I'm looking for a way to mock the url_launcher package for using inside
flutter tests.
To mock url_launcher you may:
Add plugin_platform_interface and url_launcher_platform_interface packages to dev_dependencies section in the pubspec.yaml file.
Implement the mock class. For example, with mocktail the implementation would be:
import 'package:mocktail/mocktail.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
class MockUrlLauncher extends Mock
with MockPlatformInterfaceMixin
implements UrlLauncherPlatform {}
Notice that here MockPlatformInterfaceMixin mixin is used.
Configure the mock as usual. With mocktail that might be:
MockUrlLauncher setupMockUrlLauncher() {
final mock = MockUrlLauncher();
registerFallbackValue(const LaunchOptions());
when(() => mock.launchUrl(any(), any())).thenAnswer((_) async => true);
return mock;
}
Tell url_launcher to use mocked version by setting it in UrlLauncherPlatform.instance:
final mock = setupMockUrlLauncher();
UrlLauncherPlatform.instance = mock;
This is an article that explicitly explains how to mock or fake the launchUrl function. Here is an example of how to mock it with mocktail. It also uses the ioc_container package to handle substitution with dependency injection.
import 'package:fafsdfsdf/main.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/material.dart';
class LaunchMock extends Mock {
Future<bool> call(
Uri url, {
LaunchMode? mode,
WebViewConfiguration? webViewConfiguration,
String? webOnlyWindowName,
});
}
void main() {
testWidgets('Test Url Launch', (tester) async {
//These allow default values
registerFallbackValue(LaunchMode.platformDefault);
registerFallbackValue(const WebViewConfiguration());
//Create the mock
final mock = LaunchMock();
when(() => mock(
flutterDevUri,
mode: any(named: 'mode'),
webViewConfiguration: any(named: 'webViewConfiguration'),
webOnlyWindowName: any(named: 'webOnlyWindowName'),
)).thenAnswer((_) async => true);
final builder = compose()
//Replace the launch function with a mock
..addSingletonService<LaunchUrl>(mock);
await tester.pumpWidget(
builder.toContainer()<MyApp>(),
);
//Tap the icon
await tester.tap(
find.byIcon(Icons.favorite),
);
await tester.pumpAndSettle();
verify(() => mock(flutterDevUri)).called(1);
});
}

How to mock function in flutter test

How can I mock a function in flutter and verify it has been called n times?
Ive tried implementing Mock from mockito but it only throws errors:
class MockFunction extends Mock {
call() {}
}
test("onListen is called once when first listener is registered", () {
final onListen = MockFunction();
// Throws: Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method?
when(onListen()).thenReturn(null);
bloc = EntityListBloc(onListen: onListen);
// If line with when call is removed this throws:
// Used on a non-mockito object
verify(onListen()).called(1);
});
});
As a workaround I am just manually tracking the calls:
test("...", () {
int calls = 0;
bloc = EntityListBloc(onListen: () => calls++);
// ...
expect(calls, equals(1));
});
So is there a way I can create simple mock functions for flutter tests?
What you could do is this:
class Functions {
void onListen() {}
}
class MockFunctions extends Mock implements Functions {}
void main() {
test("onListen is called once when first listener is registered", () {
final functions = MockFunctions();
when(functions.onListen()).thenReturn(null);
final bloc = EntityListBloc(onListen: functions.onListen);
verify(functions.onListen()).called(1);
});
}
The accepted answer is correct, but it doesn't represent a real-life scenario where you will probably want to substitute a top-level function with a mock or a fake. This article explains how to include top-level functions in your dependency injection composition so that you can substitute those functions with mocks.
You can compose dependency injection like this and point to top-level functions such as launchUrl with ioc_container.
IocContainerBuilder compose() => IocContainerBuilder(
allowOverrides: true,
)
..addSingletonService<LaunchUrl>(
(url, {mode, webOnlyWindowName, webViewConfiguration}) async =>
launchUrl(
url,
mode: mode ?? LaunchMode.platformDefault,
webViewConfiguration:
webViewConfiguration ?? const WebViewConfiguration(),
webOnlyWindowName: webOnlyWindowName,
),
)
..add((container) => MyApp(launchUrl: container<LaunchUrl>()));
Then, you can use the technique mentioned in the answer here to mock with Mocktail.
import 'package:fafsdfsdf/main.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/material.dart';
class LaunchMock extends Mock {
Future<bool> call(
Uri url, {
LaunchMode? mode,
WebViewConfiguration? webViewConfiguration,
String? webOnlyWindowName,
});
}
void main() {
testWidgets('Test Url Launch', (tester) async {
//These allow default values
registerFallbackValue(LaunchMode.platformDefault);
registerFallbackValue(const WebViewConfiguration());
//Create the mock
final mock = LaunchMock();
when(() => mock(
flutterDevUri,
mode: any(named: 'mode'),
webViewConfiguration: any(named: 'webViewConfiguration'),
webOnlyWindowName: any(named: 'webOnlyWindowName'),
)).thenAnswer((_) async => true);
final builder = compose()
//Replace the launch function with a mock
..addSingletonService<LaunchUrl>(mock);
await tester.pumpWidget(
builder.toContainer()<MyApp>(),
);
//Tap the icon
await tester.tap(
find.byIcon(Icons.favorite),
);
await tester.pumpAndSettle();
verify(() => mock(flutterDevUri)).called(1);
});
}

How to test nulls using mockito?

I am using mockito 4.1.3 , and here I have some test class:
import 'package:flutter_test/flutter_test.dart';
import 'package:ghinbli_app/models/film_model.dart';
import 'package:ghinbli_app/network/ghibli_films.dart';
import 'package:mockito/mockito.dart';
class MockClient extends Mock implements GhibliFilms {
#override
Future<List<FilmModel>> getFilms() async{
return null;
}
}
void main() {
final GhibliFilms ghibliMock = MockClient();
test('If API call was unsuccessful and data received is null', () {
expect(ghibliMock.getFilms(), null);
});
}
Inside the MockClient class, I am overriding a method called getFilms() and returning null to simulate a situation when a call to some API returns null as data.
A problem
When I try to check if getFilms() actually returns a null value my test will fail with this error (probably because of the return type of getFilms()):
Expected: <null>
Actual: <Instance of 'Future<List<FilmModel>>'>
How can I check and test that the data from getFilms() is actually null, what am I doing wrong?
I've tested your code and got same error as you. After making these changes everything runs fine, try it yourself.
class MockClient extends Mock implements GhibliFilms {
#override
Future<List<FilmModel>> getFilms() async {
return Future.value(null); // this is not that important
}
}
void main() {
final GhibliFilms ghibliMock = MockClient();
// async/await here was important
test('If API call was unsuccessful and data received is null', () async {
expect(await ghibliMock.getFilms(), null);
});
}

what is the correct approach to test riverpod with mockito

what is the correct approach to test riverpod with mockito?
running the code above,
/// ### edited snippets from production side ###
/// not important, skip to the TEST below!
/// this seems meaningless just because it is out of context
mixin FutureDelegate<T> {
Future<T> call();
}
/// delegate implementation
import '../../shared/delegate/future_delegate.dart';
const k_STRING_DELEGATE = StringDelegate();
class StringDelegate implements FutureDelegate<String> {
const StringDelegate();
#override
Future<String> call() async {
/// ... returns a string at some point, not important now
}
}
/// the future provider
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '<somewhere>/delegate.dart'; /// the code above
final stringProvider = FutureProvider<String>((ref) => k_STRING_DELEGATE());
/// ### edited snippets from TEST side ###
/// mocking the delegate
import 'package:mockito/mockito.dart';
import '<see above>/future_delegate.dart';
class MockDelegate extends Mock implements FutureDelegate<String> {}
/// actual test
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/all.dart';
import 'package:mockito/mockito.dart';
import '<somewhere in my project>/provider.dart';
import '../../domain/<somewhere>/mock_delegate.dart'; // <= the code above
void main() {
group('`stringProvider`', () {
final _delegate = MockDelegate();
test('WHEN `delegate` throws THEN `provider`return exception',
() async {
when(_delegate.call()).thenAnswer((_) async {
await Future.delayed(const Duration(seconds: 1));
throw 'ops';
});
final container = ProviderContainer(
overrides: [
stringProvider
.overrideWithProvider(FutureProvider((ref) => _delegate()))
],
);
expect(
container.read(stringProvider),
const AsyncValue<String>.loading(),
);
await Future<void>.value();
expect(container.read(stringProvider).data.value, [isA<Exception>()]);
});
});
}
running the test returns
NoSuchMethodError: The getter 'value' was called on null.
Receiver: null
Tried calling: value
dart:core Object.noSuchMethod
src/logic/path/provider_test.dart 28:48 main.<fn>.<fn>
I'm new to riverpod, clearly I'm missing something
I tried to follow this
I found that I had some extra errors specifically when using StateNotifierProvider. The trick was to not only override the StateNotifierProvider, but also its state property (which is a StateNotifierStateProvider object).
class SomeState {
final bool didTheThing;
SomeState({this.didTheThing = false});
}
class SomeStateNotifier extends StateNotifier<SomeState> {
SomeStateNotifier() : super(SomeState());
bool doSomething() {
state = SomeState(didTheThing: true);
return true;
}
}
final someStateProvider = StateNotifierProvider<SomeStateNotifier>((ref) {
return SomeStateNotifier();
});
class MockStateNotifier extends Mock implements SomeStateNotifier {}
void main() {
final mockStateNotifier = MockStateNotifier();
when(mockStateNotifier.doSomething()).thenReturn(true);
final dummyState = SomeState(didTheThing: true); // This could also be mocked
ProviderScope(
overrides: [
someStateProvider.overrideWithValue(mockStateProvider), // This covers usages like "useProvider(someStateProvider)"
someStateProvider.state.overrideWithValue(dummyState), // This covers usages like "useProvider(someStateProvider.state)"
],
child: MaterialApp(...),
);
}
There are 2 errors in your code
You're trying to test a throw error, so you should use thenThrow instead of thenAnswer, but because you're overriding a mixing method I would recommend instead of using Mock use Fake (from the same mockito library) to override methods and then throw it as you want
class MockDelegate extends Fake implements FutureDelegate<String> {
#override
Future<String> call() async {
throw NullThrownError; //now you can throw whatever you want
}
}
And the second problem (and the one your code is warning you) is that you deliberately are throwing, so you should expect an AsyncError instead, so calling container.read(stringProvider).data.value is an error because reading the riverpod documentation:
When calling data:
The current data, or null if in loading/error.
so if you're expecting an error (AsyncError) data is null, and because of that calling data.value its the same as writing null.value which is the error you're experiencing
This is the code you could try:
class MockDelegate extends Fake implements FutureDelegate<String> {
#override
Future<String> call() async {
throw NullThrownError;
}
}
void main() {
group('`stringProvider`', () {
final _delegate = MockDelegate();
test('WHEN `delegate` throws THEN `provider`return exception', () async {
final container = ProviderContainer(
overrides: [
stringProvider
.overrideWithProvider(FutureProvider((ref) => _delegate.call()))
],
);
expect(container.read(stringProvider), const AsyncValue<String>.loading());
container.read(stringProvider).data.value;
await Future<void>.value();
expect(container.read(stringProvider), isA<AsyncError>()); // you're expecting to be of type AsyncError because you're throwing
});
});
}
Also consider mocking out various providers by using an Override in your top level ProviderScope. That's what override can do quite well.