Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Compare FormData Objects and Write Tests in Dio Library #1888

Closed
bartosz-timecamp opened this issue Jul 5, 2023 · 4 comments
Closed
Labels
i: wontfix This will not be worked on p: dio Targeting `dio` package

Comments

@bartosz-timecamp
Copy link

Package

dio

Version

5.2.1

Output of flutter doctor -v

[!] Flutter (Channel stable, 3.10.1, on macOS 13.4.1 22F82 darwin-arm64, locale en-PL)
    • Flutter version 3.10.1 on channel stable at /Users/bartosz/fvm/versions/3.10.1
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d3d8effc68 (7 weeks ago), 2023-05-16 17:59:05 -0700
    • Engine revision b4fb11214d
    • Dart version 3.0.1
    • DevTools version 2.23.1

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] IntelliJ IDEA Community Edition (version 2022.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 71.0.6
    • Dart plugin version 223.7571.203

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.4.1 22F82 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 114.0.5735.198

[✓] Network resources
    • All expected network resources are available.

Dart Version

3.0.1

Steps to Reproduce

About the problem

The main problem I am facing right now is being unable to write a unit test for one of the POST endpoints. It requires passing FormData, but I can't compare FormData to another FormData. This limitation prevents me from writing the unit test, and I encounter the following error:

Assertion failed: "Could not find mocked route matching request
for POST /internal/api/v3/screenshot/push { data: Instance of 'FormData',
query parameters: {}, headers: {content-type: multipart/form-data;
boundary=--dio-boundary-0598433441, accept: application/json,
content-length: 294} }"

Code

Here's the code related to the issue:

@immutable
class CreateScreenshotRequest {

  const CreateScreenshotRequest({
    required this.createdAt,
    required this.screenshotBytes,
  });

  FormData toFormData() {
    return FormData.fromMap({
      'created_at': createdAt.toString(),
      'screenshot': MultipartFile.fromBytes(
        screenshotBytes,
        filename: 'name',
      ),
    });
  }

  final DateTime createdAt;

  final Uint8List screenshotBytes;

  @override
  bool operator ==(Object other) =>
      other is CreateScreenshotRequest &&
      createdAt == other.createdAt &&
      listEquals(screenshotBytes, other.screenshotBytes);

  @override
  int get hashCode => createdAt.hashCode ^ screenshotBytes.hashCode;
}

Here's how the unit test looks like:

  test('No data is returned when HTTP call is successful', () async {
    dioAdapter.onPost(
      path,
      (server) => server.reply(200, ''),
      data: createScreenshotRequest.toFormData(),
      headers: _getRequestHeaders(),
    );

    final response = await apiClient.sendScreenshot(
      request: createScreenshotRequest,
    );
    expect(response, isA<Success>());
  });

Expected Result

The main problem I am facing right now is being unable to compare FormData objects directly. The Dio library does not provide a built-in comparison method or operator overload for FormData. However, it is important for me to be able to compare one FormData object to another.

Actual Result

final createScreenshotRequest = CreateScreenshotRequest(
  createdAt: DateTime(2023, 1, 1, 10),
  screenshotBytes: Uint8List.fromList([1, 2, 3, 4, 5]),
);
final secondCreateScreenshotRequest = CreateScreenshotRequest(
  createdAt: DateTime(2023, 1, 1, 10),
  screenshotBytes: Uint8List.fromList([1, 2, 3, 4, 5]),
);

expect(
   createScreenshotRequest.toFormData(),
   secondCreateScreenshotRequest.toFormData(),
 );

package:matcher expect
package:flutter_test/src/widget_tester.dart 459:16 expect
test/client/tc_api_client_test.dart 1351:7 main..
Expected: <Instance of 'FormData'>
Actual: <Instance of 'FormData'>

@bartosz-timecamp bartosz-timecamp added h: need triage This issue needs to be categorized s: bug Something isn't working labels Jul 5, 2023
@kuhnroyal
Copy link
Member

Are you using http-mock-adapter to test? It has a matcher for FormData.

@bartosz-timecamp
Copy link
Author

I couldn't find this in the package (http_mock_adapter: ^0.4.4). Could you show it?

@AlexV525
Copy link
Member

I couldn't find this in the package (http_mock_adapter: ^0.4.4). Could you show it?

https://github.com/lomsa-dev/http-mock-adapter/blob/main/lib/src/matchers/form_data.dart

@AlexV525
Copy link
Member

Closing as #1888 (comment).

@AlexV525 AlexV525 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2023
@AlexV525 AlexV525 added i: wontfix This will not be worked on p: dio Targeting `dio` package and removed h: need triage This issue needs to be categorized s: bug Something isn't working labels Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i: wontfix This will not be worked on p: dio Targeting `dio` package
Projects
None yet
Development

No branches or pull requests

3 participants