I try to verify the state of another bloc is changed with bloc_test but not sure how to do.
The situation is like that
BlocA
has an event calledSignIn
BlocB
has an item in state calledUser
, which is updated with event calledUserChanged
I would like to verify that when BlocA.add(SignIn()), the User in BlocB is updated with specific values.
My current idea is
act: (blocA) {
blocA.add(SignIn());
},
verify: (bloc) {
verify(() => BlocB.add(
UserChanged(
user: any(
named: "user",
that: isA<User>().having(
(user) => user.email,
"email",
"[email protected]",
),
),
),
)).called(1);
},
But I got the following error
Invalid argument(s): An ArgumentMatcher was declared as named user, but was not passed as an argument named user.
BAD: when(() => obj.fn(any(named: "a")))
GOOD: when(() => obj.fn(a: any(named: "a")))
Please advice how I can achieve it. Thank you
===============================================
Update: my test will pass if I do not verify the argument passed to UserChanged
event, such as
act: (blocA) {
blocA.add(SignIn());
},
verify: (bloc) {
verify(() => BlocB.add(any<UserChanged>())).called(1);
},
2
Answers
I think it’s not the best idea to test the way you want.
I would prefer to have 3 tests:
yes, at least I did the same.
You need to check the proper states and method execution separately.
So the test above is for that use case: