如何使用网络相对存储仪进行单位测试集团?

发布于 2025-01-24 19:16:14 字数 530 浏览 2 评论 0原文

我创建一个bloc调用身份验证。此集团发出两个状态调用自动启动 基于AuthenticationRepository,将其依赖性注入BLOC。

然后,我想为单位测试创建模拟的ocerauthenticationRepository,但是我得到了两个不同的状态,因此我必须创建两个不同版本的存储库,例如oige> oigaautenticatiCatiCatiCatiCateCationAuthenticationRepositorymockunAuthententicateTicatiCatiCatiCatiCatiCateCationRicationRepository代码>测试这两种情况。

但是,也许在存储库上还有另一个集合5或6个州基础,然后我必须为其创建5至6个模拟的存储库。这听起来并不是很大的错误,因为一个集团不会生长无限。但是我仍在寻找更好的方法来解决这个问题。有人有更好的主意吗?

I create a bloc call Authentication. This bloc emit two state call Autenticated and UnAuthenticated base on AuthenticationRepository which dependency injected into bloc.

Then I tring to create a mocked MockAuthenticationRepository for unit test but I got two different state so I have to create two different version of repository like MockAutenticatedAuthenticationRepository and MockUnAuthenticatedAuthenticationRepository to test these two case.

But maybe there is a another bloc emit 5 or 6 state base on repository then i have to create 5 to 6 mocked repository for it. This sound not terribly wrong because one bloc shondn't growing unlimit. But I still looking for better way to solve this. Anyone got better idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2025-01-31 19:16:14

您可以使用“ https://pub.dev/packages/mocktail”“ rel =” nofollow noreferrer“> https://pub.dev/packages/mocktail )存储库的正确行为。
使用软件包bloc_test( https://pub.dev/packages/bloc_test )实现集体单元测试。

import 'package:mocktail/mocktail.dart';

class MockAutenticatedAuthenticationRepository extends Mock implements AutenticatedAuthenticationRepository {};

void main() {
  late MockAutenticatedAuthenticationRepository authMock;
  late AuthBloc bloc;

  setUp(() {
    authMock = MockAutenticatedAuthenticationRepository();
    bloc = AuthBloc(auchRepository: authMock);
  });


 blocTest(
      'should emit [AuthLoadingState, AuthSuccessState] when login data is correct',
      build: () {
        when(() => authMock.doAuth(any())).thenAnswer((_) async => true);
        return bloc;
      },
      act: (dynamic b) => b.add(AuthEvent.login("username","password")),
      expect: () => [AuthState.loading(), AuthState.success()],
    );

   blocTest(
      'should emit [AuthLoadingState, AuthFailureState] when login data is incorrect',
      build: () {
        when(() => authMock.doAuth(any())).thenAnswer((_) async => false);
        return bloc;
      },
      act: (dynamic b) => b.add(AuthEvent.login("username","wrong password")),
      expect: () => [AuthState.loading(), AuthState.failure()],
    );
}

您必须调整方法调用,依此类推,因为我没有示例代码。但是我认为这应该有助于您解决问题。

you can use the package mocktail (https://pub.dev/packages/mocktail) to set up the correct behavior of the repository.
It´s also a good idea to use the package bloc_test (https://pub.dev/packages/bloc_test) to implement bloc unit tests.

import 'package:mocktail/mocktail.dart';

class MockAutenticatedAuthenticationRepository extends Mock implements AutenticatedAuthenticationRepository {};

void main() {
  late MockAutenticatedAuthenticationRepository authMock;
  late AuthBloc bloc;

  setUp(() {
    authMock = MockAutenticatedAuthenticationRepository();
    bloc = AuthBloc(auchRepository: authMock);
  });


 blocTest(
      'should emit [AuthLoadingState, AuthSuccessState] when login data is correct',
      build: () {
        when(() => authMock.doAuth(any())).thenAnswer((_) async => true);
        return bloc;
      },
      act: (dynamic b) => b.add(AuthEvent.login("username","password")),
      expect: () => [AuthState.loading(), AuthState.success()],
    );

   blocTest(
      'should emit [AuthLoadingState, AuthFailureState] when login data is incorrect',
      build: () {
        when(() => authMock.doAuth(any())).thenAnswer((_) async => false);
        return bloc;
      },
      act: (dynamic b) => b.add(AuthEvent.login("username","wrong password")),
      expect: () => [AuthState.loading(), AuthState.failure()],
    );
}

You have to adapt the method calls and so on, because i don´t have your example code. But i think that should help you solving your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文