英文:
Why state is not updating in bloc_test?
问题
以下是您要的翻译部分:
"I am new at testing of flutter app, so I am trying to find out how to test it correctly, and now I am confused when I faced this error. When I try to change state in bloc_state testing then state doesn't change at all. So how can I fix that?"
"我是 Flutter 应用测试的新手,所以我正在努力找出如何正确测试它,但当我遇到这个错误时,我感到困惑。在进行 bloc_state 测试时,当我尝试更改状态时,状态根本不会改变。我该如何修复这个问题?"
"class MockAuth extends MockBloc<AuthCubit, AuthState> implements AuthCubit {}"
"class MockAuth 继承自 MockBloc<AuthCubit, AuthState> 并实现了 AuthCubit 接口。"
"void main() {
blocTest(
build: () => MockAuth(), // If I change MockAuth to AuthCubit then it starts to work
act: (bloc) async {
bloc.setEmail("secret@email.com");
},
expect: () => [
AuthState(
email: "secret@email.com",
password: "",
firstName: "",
lastName: ""
...
)
]
);
}"
"void main() {
blocTest(
build: () => MockAuth(), // 如果我将 MockAuth 更改为 AuthCubit,它就开始工作
act: (bloc) async {
bloc.setEmail("secret@email.com");
},
expect: () => [
AuthState(
email: "secret@email.com",
password: "",
firstName: "",
lastName: ""
...
)
]
);
}"
"This is my cubit:"
"这是我的 cubit:"
"class AuthCubit extends Cubit<AuthState> {
AuthCubit() : super(const AuthState(
email: "",
password: "",
firstName: "",
lastName: "",
genderUuid: "",
ageGroupUuid: "",
countryUuid: "",
forgotEmail: "",
code: 0,
));
void setEmail(String email) => emit(state.copyWith(email: email));
}"
"class AuthCubit 继承自 Cubit
英文:
I am new at testing of flutter app, so I am trying to find out how to test it correctly, and now I am confused when I faced this error. When I try to change state in bloc_state testing then state doesn't change at all. So how can I fix that?
class MockAuth extends MockBloc<AuthCubit, AuthState> implements AuthCubit {}
void main() {
blocTest(
build: () => MockAuth(), // If I change MockAuth to AuthCubit then it starts to work
act: (bloc) async {
bloc.setEmail("secret@email.com");
},
expect: () => [
AuthState(
email: "secret@email.com",
password: "",
firstName: "",
lastName: ""
...
)
]
);
}
This is my cubit:
class AuthCubit extends Cubit<AuthState> {
AuthCubit() : super(const AuthState(
email: "",
password: "",
firstName: "",
lastName: "",
genderUuid: "",
ageGroupUuid: "",
countryUuid: "",
forgotEmail: "",
code: 0,
));
void setEmail(String email) => emit(state.copyWith(email: email));
}
答案1
得分: 1
我猜你正在查阅bloc_test的文档,这很好,但可能会令人困惑。对于这种情况,你不需要模拟你的bloc/cubit,因为你想要测试它的实际行为和逻辑。
所以,在build
参数中,你传递blocTest
一个你的bloc/cubit的实际实例,而在act
参数中,你添加/调用一个事件/方法到你的bloc/cubit,然后期望bloc发出不同的状态。
如果你的cubit的任何方法依赖于存储库请求或任何其他服务,你应该模拟该存储库/服务以获取预期的结果和预期的状态。
但这不是这种情况,所以你的测试应该看起来像这样:
blocTest<AuthCubit, AuthState>(
build: () => AuthCubit(),
act: (bloc) async {
bloc.setEmail("secret@email.com");
},
expect: () => [
AuthState(
email: "secret@email.com",
password: "",
firstName: "",
lastName: "",
...
)
]
);
英文:
I guess you were following the bloc_test documentation which is great but can be confusing. For this case scenario, you don't want to mock your bloc/cubit, since you want to test its actual behavior and logic.
So, on the build parameter, you pass the blocTest a real instance of your bloc/cubit, and on the act parameter, you add/invoke an event/method to your bloc/cubit, then expect the bloc to emit different states.
In case any of your cubit methods depends on a repository request or any other service, you should mock that repository/service to get the expected result and the expected state emitted.
But this is not the case, so your test should look something like this:
blocTest<AuthCubit, AuthState>(
build: () => AuthCubit(),
act: (bloc) async {
bloc.setEmail("secret@email.com");
},
expect: () => [
AuthState(
email: "secret@email.com",
password: "",
firstName: "",
lastName: ""
...
)
]
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论