// State
class Counter extends Equatable{
final int value;
Counter(this.value);
@override
List<Object> get props => [value];
@override
bool get stringify => true;
Counter increment() => Counter(this.value + 1);
Counter decrement() => Counter(this.value - 1);
}
increment()
and decrement()
this way, by returning a new instance
void main() {
group("AccountLinking Screen", () {
MockUserDataSourceBloc mockUserDataSourceBloc;
NavigatorObserver mockNavigatorObserver;
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized();
mockUserDataSourceBloc = di.sl<UserDataSourceBloc>();
mockNavigatorObserver = MockNavigatorObserver();
});
tearDown(() {
mockUserDataSourceBloc.close();
});
Future<void> _buildApp(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
title: "Test App",
home: Scaffold(
body: BlocProvider<UserDataSourceBloc>(
create: (_) => mockUserDataSourceBloc,
child: UserDataSourceView(),
),
),
navigatorObservers: [mockNavigatorObserver],
routes: routes,
),
);
}
testWidgets("should update datasource list when linking succeeds",
(WidgetTester tester) async {
final expectedStates = [
UserDataSourceInitial(),
GetUserDataSourcesLoaded(userDataSources: []),
LinkUserDataSourceLoading(),
LinkUserDataSourceLoaded(
userDataSource: UserDataSource(
userDataSourceId: "1",
userId: "uuid",
status: DataSourceStatus.LINKED,
),
),
];
whenListen(
mockUserDataSourceBloc,
Stream.fromIterable(expectedStates),
);
await _buildApp(tester);
});
});
}
class JournalBloc extends Bloc<JournalEvent, JournalState> {
final JournalRepository journalRepository;
final AddJournalBloc addJournalBloc;
StreamSubscription subscription;
JournalBloc({@required this.journalRepository, this.addJournalBloc})
: super(JournalInitial()) {
subscription = addJournalBloc.listen((state) {
print("I AM LISTENING");
if (state is AddJournalSuccess) {
print("I AM LISTENING AND FIRED");
add(LoadJournal(keyword: '', pageno: 0));
}
});
}
@override
JournalState get initialState => JournalInitial();
@override
Stream<JournalState> mapEventToState(
JournalEvent event,
) async {
if (event is LoadJournal) {
yield _mapLoadJournalToState(event);
}
}
Stream<JournalState> mapLoadJournalToState(LoadJournal event) async* {
print("I AM LISTENING AND FIRED INSIDE STATA");
yield JournalLoading();
List<Journal> journal;
try {
journal = await journalRepository.getJournalList();
print("LENGTH:" + journal[0].title);
if (journal.length == 0) {
yield JournalEmpty();
} else {
yield JournalLoaded(journal: journal);
}
} catch () {
yield JournalError();
}
}
@override
Future<void> close() {
subscription.cancel();
return super.close();
}
}
hello
test(
'should emit [Error] when the input is invalid',
() async {
//arrange
when(mockInputConverter.stringToUnsignedInteger(any))
.thenReturn(Left(InvalidInputFailure()));
//assert later
final expected = [
Empty(),
Error(message: INVALID_INPUT_FAILURE_MESSAGE),
];
expectLater(
bloc,
emitsInOrder(
expected,
));
//act
bloc.add(GetTriviaForConcreteNumber(tNumberString));
},
);
@override
Stream<NumberTriviaState> mapEventToState(
NumberTriviaEvent event,
) async* {
if (event is GetTriviaForConcreteNumber) {
final inputEither =
inputConverter.stringToUnsignedInteger(event.numberString);
yield* inputEither.fold(
(failure) async* {
yield Error(message: INVALID_INPUT_FAILURE_MESSAGE);
},
(integer) async* {
yield null;
},
);
}
}
i tried to resolve this problem but in vain
i get this error
Expected: should do the following in order:
• emit an event that Empty:<Empty()>
• emit an event that Error:<Error(Invalid Input - The number must be a positive integer or zero.)>
Actual: <Instance of 'NumberTriviaBloc'>
Which: was not a Stream or a StreamQueue
runApp(
MultiBlocProvider(
providers: [
BlocProvider<TestCubit>(lazy: false, create: (context) => TestCubit())
],
child: Builder(
builder: (context) {
return const MyApp();
},
),
),
)