Closed ghost closed 4 years ago
I can't really go through all this code. Try to create a very simple example, in a single file with a main method. I must be very simple, at most 30 lines if you want me to have a look at it.
But, at first glance, you are creating the store twice, which is clearly wrong:
New StoreTester.
New StoreTester.
I think instead of this:
store: createStoreTester().store,
It should be this:
store: storeTester.store,
The duplicate StoreTester initialization was the problem, I should have sleep on this to notice that. Anyway I created the single file test, I leave it there for future reference. Definitely not 30 lines.
import 'package:async_redux/async_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
class MockRepository extends Mock implements Repository {}
class AppState {
String text;
AppState({this.text});
AppState.empty();
static AppState initialState() => AppState.empty();
AppState copy({String text}) {
return AppState(text: text);
}
}
class Action1 extends ReduxAction<AppState> {
final Repository repository = Repository();
@override
Future<AppState> reduce() async {
var s = await repository.doSomething();
return state.copy(text: s);
}
}
class MyWidget extends StatelessWidget {
final void Function() fireAction;
MyWidget({this.fireAction});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Connector test'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[],
),
),
floatingActionButton: FloatingActionButton(
key: Key('my-button'),
onPressed: fireAction,
tooltip: 'Fire action',
),
);
}
}
class MyConnector extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreConnector<AppState, ViewModel>(
model: ViewModel(),
builder: (BuildContext context, ViewModel vm) => MyWidget(
fireAction: vm.fireAction,
));
}
}
class ViewModel extends BaseModel<AppState> {
ViewModel();
void Function() fireAction;
ViewModel.build({@required this.fireAction});
@override
BaseModel fromStore() =>
ViewModel.build(fireAction: () => dispatch(Action1()));
}
class Repository {
http.Client client = http.Client();
Future<String> doSomething() async {
await callAPI();
return 'Answer from API call';
}
Future<http.Response> callAPI() async => await client
.post('http://192.168.0.137:7071/api/login', body: 'Request body');
}
void main() {
StoreTester<AppState> createStoreTester() {
var store = Store<AppState>(initialState: AppState.initialState());
return StoreTester.from(store);
}
MockRepository repository;
setUp(() {
repository = MockRepository();
});
testWidgets('LoginUserAction test', (WidgetTester tester) async {
when(repository.doSomething()).thenAnswer((_) async => 'answer');
var storeTester = createStoreTester();
await tester.pumpWidget(
StoreProvider<AppState>(
store: storeTester.store, child: MaterialApp(home: MyConnector())),
);
var buttonFinder = find.byKey(Key('my-button'));
await tester.tap(buttonFinder);
await tester.pump();
var info = await storeTester.wait(Action1);
expect(info.dispatchCount, 1);
});
}
I didn't find any tests related to Connectors in examples or in the repository, so I started implementing myself.
I expect that after tapping the Login button, LoginUserAction is dispatched. The console prints out the intialization(INI) and end(END) of the LoginUserAction, but the test is running for minutes and times out. My repository is mocked out. What I do wrong?
LoginPageConnector
UserRepository
LoginUserAction
login_page_CONNECTOR_test
Console output