为什么状态在bloc_test中没有更新?

huangapple go评论68阅读模式
英文:

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: &quot;&quot;,
code: 0,

));

void setEmail(String email) => emit(state.copyWith(email: email));
}"

"class AuthCubit 继承自 Cubit 类。AuthCubit() 构造函数中初始化了一个 AuthState 实例,包括 email、password、firstName、lastName 等属性。 setEmail 方法用于设置 email 属性并触发状态更新。"

英文:

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&lt;AuthCubit, AuthState&gt; implements AuthCubit {}

void main() {
  blocTest(
    build: () =&gt; MockAuth(), // If I change MockAuth to AuthCubit then it starts to work
    act: (bloc) async {
      bloc.setEmail(&quot;secret@email.com&quot;);
    },
    expect: () =&gt; [
      AuthState(
        email: &quot;secret@email.com&quot;,
        password: &quot;&quot;,
        firstName: &quot;&quot;,
        lastName: &quot;&quot;
        ...
      )
    ]
  );
}

This is my cubit:

class AuthCubit extends Cubit&lt;AuthState&gt; {
  AuthCubit() : super(const AuthState(
    email: &quot;&quot;,
    password: &quot;&quot;,
    firstName: &quot;&quot;,
    lastName: &quot;&quot;,
    genderUuid: &quot;&quot;,
    ageGroupUuid: &quot;&quot;,
    countryUuid: &quot;&quot;,

    forgotEmail: &quot;&quot;,
    code: 0,

  ));

  void setEmail(String email) =&gt; 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&lt;AuthCubit, AuthState&gt;(
    build: () =&gt; AuthCubit(),
    act: (bloc) async {
      bloc.setEmail(&quot;secret@email.com&quot;);
    },
    expect: () =&gt; [
      AuthState(
        email: &quot;secret@email.com&quot;,
        password: &quot;&quot;,
        firstName: &quot;&quot;,
        lastName: &quot;&quot;
        ...
      )
    ]
  );

huangapple
  • 本文由 发表于 2023年2月8日 22:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定