Created
April 9, 2025 17:58
-
-
Save slightfoot/07f126b2a15b898c03f5479d8cbdaabe to your computer and use it in GitHub Desktop.
Mocking with noSuchMethod - by Simon Lightfoot :: #HumpdayQandA on 9th April 2025 :: https://www.youtube.com/watch?v=LddUVHziMaM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2025 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp( | |
App(backend: Backend()), | |
); | |
} | |
class AppConfig { | |
AppConfig(this._config); | |
final Map<String, dynamic> _config; | |
String get welcomeMessage => _config['welcome_message']; | |
} | |
class Api { | |
Api(); | |
Future<Map<String, dynamic>> fetchAppConfig() async { | |
// TODO: go to server and fetch content | |
return { | |
'welcome_message': 'Welcome', | |
}; | |
} | |
} | |
class Backend { | |
Backend({Api? api}) { | |
this.api = api ?? Api(); | |
} | |
late final Api api; | |
AppConfig? _config; | |
AppConfig get config => _config!; | |
Future<void> loadApp() async { | |
_config = AppConfig(await api.fetchAppConfig()); | |
await Future.delayed(const Duration(milliseconds: 2000)); | |
} | |
void dispose() { | |
// Cleanup backend | |
} | |
} | |
class App extends StatefulWidget { | |
const App({ | |
super.key, | |
this.backend, | |
}); | |
final Backend? backend; | |
static AppConfig appConfigOf(BuildContext context) { | |
return context.findAncestorStateOfType<_AppState>()!.backend.config; | |
} | |
@override | |
State<App> createState() => _AppState(); | |
} | |
class _AppState extends State<App> { | |
late Backend backend; | |
late Future<void> _appLoader; | |
@override | |
void initState() { | |
super.initState(); | |
backend = widget.backend ?? Backend(); | |
_appLoader = backend.loadApp(); | |
} | |
@override | |
void dispose() { | |
backend.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark(), | |
home: FutureBuilder( | |
future: _appLoader, | |
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) { | |
return snapshot.connectionState != ConnectionState.done // | |
? Splash() | |
: Home(); | |
}, | |
), | |
); | |
} | |
} | |
class Splash extends StatelessWidget { | |
const Splash({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
final theme = Theme.of(context); | |
return Material( | |
color: theme.hintColor, | |
child: Placeholder( | |
color: theme.highlightColor, | |
), | |
); | |
} | |
} | |
class Home extends StatelessWidget { | |
const Home({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Material( | |
child: Center( | |
child: Text( | |
App.appConfigOf(context).welcomeMessage, | |
style: TextStyle( | |
fontSize: 32.0, | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2025 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:flutter_test_app2/humpday_2025-04-09-mock.dart'; | |
void main() { | |
testWidgets( | |
'Testing welcome message of app', | |
(tester) async { | |
await tester.pumpWidget(App()); | |
await tester.pump(const Duration(seconds: 3)); | |
expect(find.text('Welcome'), findsOneWidget); | |
}, | |
); | |
testWidgets( | |
'Testing alternative welcome message of app', | |
(tester) async { | |
final api = MockApi(); | |
await tester.pumpWidget( | |
App(backend: Backend(api: api)), | |
); | |
await tester.pump(const Duration(seconds: 3)); | |
expect( | |
find.text('Welcome'), | |
findsExactly(0), | |
reason: 'Welcome message exists when it shouldn\'t', | |
); | |
expect( | |
find.text('Alternative'), | |
findsOneWidget, | |
reason: 'Alternative welcome text missing', | |
); | |
}, | |
); | |
} | |
class MockApi implements Api { | |
MockApi(); | |
@override | |
dynamic noSuchMethod(Invocation invocation) { | |
if (invocation.isMethod && invocation.memberName == #fetchAppConfig) { | |
return Future.value(<String, dynamic>{ | |
'welcome_message': 'Alternative', | |
}); | |
} | |
return super.noSuchMethod(invocation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment