Used on a non-mocktail object

223 Views Asked by At
Used on a non-mocktail object
package:matcher                                                         fail
package:mocktail/src/mocktail.dart 522:7                                _makeVerify.<fn>
test\features\auth\data\datasources\remote_data_source_test.dart 69:15  main.<fn>.<fn>.<fn>

Using Mocktail verify function I am trying to verify that data posted by my test function is same as data posted by mock Dio created by Mocktail. This verification includes async data which should be posted by dio, which is MultipartFile. Without this MultipartFile my code works fine and it verifies as it is suppose to.

But when I try to verify with MultipartFile it throws this error because I've made verify function async.

test("It should post loginUser and return void by calling only once", () {
        //Arrange
        when(() => dio.post(
                  any(),
                  data: any(named: "data"),
                ))
            .thenAnswer((invocation) async => Response(
                requestOptions: RequestOptions(),
                statusCode: 201,
                data: {"message": "user logged in sucessfully"}));
        //Act
        final response = remoteDatasource.loginUser(
            name: params.name,
            phoneNumber: params.phoneNumber,
            countryCode: params.countryCode,
            image: params.image);
        //Assert
        expect(response, completes);
        verify(() async => dio.post(dio.options.baseUrl + loginUrl,
            data: FormData.fromMap({
              "image": params.image != null
                  ? await MultipartFile.fromFile(params.image!.path,
                      filename: imageFileName)
                  : null,
              "name": params.name,
              "phoneNumber": params.phoneNumber,
              "countryCode": params.countryCode
            }))).called(1);
        verifyNoMoreInteractions(dio);
      });

when I do not use async it throws this error:

No matching calls (actually, no calls at all).
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)
package:matcher                                                         fail
package:mocktail/src/mocktail.dart 728:7                                _VerifyCall._checkWith
package:mocktail/src/mocktail.dart 519:18                               _makeVerify.<fn>
test\features\auth\data\datasources\remote_data_source_test.dart 69:15  main.<fn>.<fn>.<fn>

definitely it won't verify because now MultipartFile is going as an Future and in my actual function await exists, which is:

Future<void> loginUser(
      {required String name,
      required int phoneNumber,
      required String countryCode,
      File? image}) async {
    try {
      final String url = _dio.options.baseUrl + loginUrl;
      final MapData data = {
        'image': image != null
            ? await MultipartFile.fromFile(
                image.path,
                filename: imageFileName,
              )
            : null,
        "name": name,
        "phoneNumber": phoneNumber,
        "countryCode": countryCode
      };

      final response = await _dio.post(
        url,
        data: FormData.fromMap(data),
      );
      print(url.toString());
      if (response.statusCode == 200 || response.statusCode == 201) {
        return;
      } else {
        throw ApiException(
            message: response.data.message,
            statusCode: response.data.statusCode);
      }
    } on DioException catch (dioException) {
      throw ApiException(
          message: dioException.error.toString(), statusCode: 500);
    } catch (e) {
      throw ApiException(message: e.toString(), statusCode: 500);
    }
  }

Note: params and dio are working fine, when image is absent.

0

There are 0 best solutions below