2

I am writing a bloc test for a feature in my flutter project. I need to code a demo object for a class that I could test in the BlocTest function. Below is a piece of code I wrote for it.

      blocTest<ClientBloc, ClientState>(
        'emits [ClientLoadSuccess, ClientCreateSuccess] '
        'state when successfully created client',
        setUp: () {
          PrivateClientModel client = { } as PrivateClientModel;
        },
        build: () => ClientBloc(authenticationBloc: authenticationBloc),
        act: (ClientBloc bloc) => bloc.add(
          const ClientCreateClient(375918, PrivateClientModel()),
        ),
        expect: () => <dynamic>[
          const ClientCreateSuccess(),
        ],
      );

In the above code I need to add an object of PrivateClientModel class whose code lies below. I tried doing some stuff but it did not work so asking here for a good approach.

class PrivateClientModel extends PrivateClientModelParent{
  final int id;
  final List<TestModel> activeTests;
  @JsonKey(name: 'created_at')
  final DateTime? createdAt;
  final String? businessUid;
  final String? uid;
  final String? receivingFacility;
  final String? firstName;
  final String? lastName;
  final String? username;
  final String? birthDate;
  final String? result;
  final String? ssnr;
  @JsonKey(name: 'insuranceCarrierIdentifier')
  final String? insurance;
  final String? telephoneNumber;
  final String? email;
  @JsonKey(name: 'latestPlacerOrderNumber')
  final String? placerOrderNumber;
  final String? eventNumber;
  final String? testIdentifier;
  final String? gender;
  final String? street;
  final String? houseNumber;
  final String? zipCode;
  final String? city;
  @JsonKey(name: 'country_id')
  final int? countryId;


  final int? redeemable;
  @JsonKey(name: 'next_redeemable')
  final DateTime? nextRedeemable;
  final int? atHome;
  @JsonKey(name: 'next_testable')
  final DateTime? nextTestable;
  final bool isMine;
  final bool useAuthUser;

  PrivateClientModel(
    this.id,
    this.activeTests, {
    this.createdAt,
    this.uid,
    this.businessUid,
    this.receivingFacility,
    this.firstName,
    this.lastName,
    this.username,
    this.birthDate,
    this.result,
    this.ssnr,
    this.insurance,
    this.telephoneNumber,
    this.email,
    this.placerOrderNumber,
    this.eventNumber,
    this.testIdentifier,
    this.gender,
    this.street,
    this.houseNumber,
    this.zipCode,
    this.city,
    this.countryId,
    this.redeemable,
    this.nextRedeemable,
    this.atHome,
    this.nextTestable,
    this.isMine = true,
    this.useAuthUser = false,
  });

  factory PrivateClientModel.fromJson(Map<String, dynamic> json) => _$PrivateClientModelFromJson(json);

  Map<String, dynamic> toJson() => _$PrivateClientModelToJson(this);

  PrivateClientModel copyWith({
    int? id,
    List<TestModel>? activeTests,
    DateTime? createdAt,
    String? uid,
    String? businessUid,
    String? receivingFacility,
    String? firstName,
    String? lastName,
    String? username,
    String? birthDate,
    String? result,
    String? ssnr,
    String? insurance,
    String? telephoneNumber,
    String? email,
    String? placerOrderNumber,
    String? eventNumber,
    String? testIdentifier,
    String? gender,
    String? street,
    String? houseNumber,
    String? zipCode,
    String? city,
    int? countryId,
    int? redeemable,
    DateTime? nextRedeemable,
    int? atHome,
    DateTime? nextTestable,
    bool? isMine,
    bool? useAuthUser,
  }) {
    return PrivateClientModel(
      id ?? this.id,
      activeTests ?? this.activeTests,
      createdAt: createdAt ?? this.createdAt,
      uid: uid ?? this.uid,
      businessUid: businessUid ?? this.businessUid,
      receivingFacility: receivingFacility ?? this.receivingFacility,
      firstName: firstName ?? this.firstName,
      lastName: lastName ?? this.lastName,
      username: username ?? this.username,
      birthDate: birthDate ?? this.birthDate,
      result: result ?? this.result,
      ssnr: ssnr ?? this.ssnr,
      insurance: insurance ?? this.insurance,
      telephoneNumber: telephoneNumber ?? this.telephoneNumber,
      email: email ?? this.email,
      placerOrderNumber: placerOrderNumber ?? this.placerOrderNumber,
      eventNumber: eventNumber ?? this.eventNumber,
      testIdentifier: testIdentifier ?? this.testIdentifier,
      gender: gender ?? this.gender,
      street: street ?? this.street,
      houseNumber: houseNumber ?? this.houseNumber,
      zipCode: zipCode ?? this.zipCode,
      city: city ?? this.city,
      countryId: countryId ?? this.countryId,
      redeemable: redeemable ?? this.redeemable,
      nextRedeemable: nextRedeemable ?? this.nextRedeemable,
      atHome: atHome ?? this.atHome,
      nextTestable: nextTestable ?? this.nextTestable,
      isMine: isMine ?? this.isMine,
      useAuthUser: useAuthUser ?? this.useAuthUser,
    );
  }
}

It would be really helpful if someone could help me on how I can create a sample object from this class that I could use in the test.

1 Answer 1

1

You can use https://pub.dev/packages/mocktail or https://pub.dev/packages/mockito to create mock objects. Mockito needs code generation but Mocktail does not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.