最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - The argument type 'AuthState' can't be assigned to the parameter type 'AuthState Funct

programmeradmin1浏览0评论

I am trying to test how is my widget reacting to BloC state changes.

This is how my test look like:

class MockAuthBloc extends Mock implements AuthBloc {}

void main() {
  initHydratedStorage();
  FlavorConfig(
      flavor: Flavor.MOCK,
      baseUrl: ''
  );
  late MockAuthBloc authBloc;
  late User user;

  setUp(() {
    authBloc = MockAuthBloc();
    user = User(
        email: '[email protected]',
        name: 'mock',
        hasCompletedProfile: true,
        language: 'pl',
        id: 1
    );
  });

  testWidgets('navigates to MainNavigator when authenticated', (tester) async {
    when(() => authBloc.state).thenReturn(AuthState.authenticated(user));

    await tester.pumpWidget(
      MaterialApp(
        home: BlocProvider<AuthBloc>.value(
          value: authBloc,
          child: const LoginPage(),
        ),
      ),
    );

    await tester.pumpAndSettle();
    expect(find.byType(MainNavigator), findsOneWidget);
  });
}

but when(() => authBloc.state).thenReturn(AuthState.authenticated(user)); is giving me error The argument type 'AuthState' can't be assigned to the parameter type 'AuthState Function()'.

and I do not know how to handle it properly.

Here is how my AuthState class looking:

part of 'auth_bloc.dart';

enum AuthStatus { unknown, authenticated, guest, failure, semiAuthenticated}


class AuthState {
  final AuthStatus status;
  final bool isFirstEntry;
  final bool isLoadingLong;
  final String? errorMessage;
  final String? email;
  final User? user;

  const AuthState({
    this.status = AuthStatus.unknown,
    this.isFirstEntry = true,
    this.isLoadingLong = false,
    this.errorMessage,
    this.email,
    this.user
  });

  const AuthState.unknown() : this();

  const AuthState.authenticated(User user)
      : this(
    status: AuthStatus.authenticated,
    user: user,
    isFirstEntry: false,
    isLoadingLong: false
  );

Maybe I am testing it completely wrong, but I am new to testing, I am trying to learn it, but I have some problem with mocking.

EDIT:

I tried different approaches and get different errors for them:

when(() => authBloc.state).thenReturn(() => AuthState(
      status: AuthStatus.authenticated,
      isFirstEntry: false,
      isLoadingLong: false,
      user: user
    ));

gives me Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method?

when(authBloc.state).thenReturn(AuthState(
      status: AuthStatus.authenticated,
      isFirstEntry: false,
      isLoadingLong: false,
      user: user
    ));

gives me type 'Null' is not a subtype of type 'AuthState'

Looks like nothing is working for me and it is driving me crazy

I am trying to test how is my widget reacting to BloC state changes.

This is how my test look like:

class MockAuthBloc extends Mock implements AuthBloc {}

void main() {
  initHydratedStorage();
  FlavorConfig(
      flavor: Flavor.MOCK,
      baseUrl: 'https://mock.api'
  );
  late MockAuthBloc authBloc;
  late User user;

  setUp(() {
    authBloc = MockAuthBloc();
    user = User(
        email: '[email protected]',
        name: 'mock',
        hasCompletedProfile: true,
        language: 'pl',
        id: 1
    );
  });

  testWidgets('navigates to MainNavigator when authenticated', (tester) async {
    when(() => authBloc.state).thenReturn(AuthState.authenticated(user));

    await tester.pumpWidget(
      MaterialApp(
        home: BlocProvider<AuthBloc>.value(
          value: authBloc,
          child: const LoginPage(),
        ),
      ),
    );

    await tester.pumpAndSettle();
    expect(find.byType(MainNavigator), findsOneWidget);
  });
}

but when(() => authBloc.state).thenReturn(AuthState.authenticated(user)); is giving me error The argument type 'AuthState' can't be assigned to the parameter type 'AuthState Function()'.

and I do not know how to handle it properly.

Here is how my AuthState class looking:

part of 'auth_bloc.dart';

enum AuthStatus { unknown, authenticated, guest, failure, semiAuthenticated}


class AuthState {
  final AuthStatus status;
  final bool isFirstEntry;
  final bool isLoadingLong;
  final String? errorMessage;
  final String? email;
  final User? user;

  const AuthState({
    this.status = AuthStatus.unknown,
    this.isFirstEntry = true,
    this.isLoadingLong = false,
    this.errorMessage,
    this.email,
    this.user
  });

  const AuthState.unknown() : this();

  const AuthState.authenticated(User user)
      : this(
    status: AuthStatus.authenticated,
    user: user,
    isFirstEntry: false,
    isLoadingLong: false
  );

Maybe I am testing it completely wrong, but I am new to testing, I am trying to learn it, but I have some problem with mocking.

EDIT:

I tried different approaches and get different errors for them:

when(() => authBloc.state).thenReturn(() => AuthState(
      status: AuthStatus.authenticated,
      isFirstEntry: false,
      isLoadingLong: false,
      user: user
    ));

gives me Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method?

when(authBloc.state).thenReturn(AuthState(
      status: AuthStatus.authenticated,
      isFirstEntry: false,
      isLoadingLong: false,
      user: user
    ));

gives me type 'Null' is not a subtype of type 'AuthState'

Looks like nothing is working for me and it is driving me crazy

Share Improve this question edited Feb 7 at 7:52 Karol Wiśniewski asked Feb 6 at 14:02 Karol WiśniewskiKarol Wiśniewski 5081 gold badge9 silver badges24 bronze badges 2
  • I looks like you are passing a () => authBloc.state, when authBloc.state was expected. This might be relevant: How to manually override a method with a non-nullable return type. – Dan R Commented Feb 6 at 17:10
  • @DanR ok, but I still don't know how I should write my when statement, I tried different things, but I still get different errors from Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method? to type 'Null' is not a subtype of type 'AuthState' I really don't know how I should write this to mock my state correctly ``` I edited my question with few other approaches – Karol Wiśniewski Commented Feb 7 at 7:49
Add a comment  | 

1 Answer 1

Reset to default 0

The documentation I mentioned in the comment manually override a method with a non-nullable return type suggests creating a stub for the getter returning a non-nullable type.

A minimal test file would look like this:

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';


class AuthBloc {
  String state = 'loading';
}

class MockAuthBloc extends Mock implements AuthBloc {

  // Stub for a getter returning a non-null type
  @override
  String get state => super.noSuchMethod(
        Invocation.getter(#state),
        returnValue: 'Mockito: String from stub: state',
      );
}

void main() {
  testWidgets('AuthStatus status test', (WidgetTester tester) async {
    final authBloc = MockAuthBloc();

    when(authBloc.state).thenReturn('authenticated');

    expect(authBloc.state, 'authenticated');
    
  });
}

The test above completes with:

$ flutter test
00:04 +1: All tests passed!                   

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论