website icon indicating copy to clipboard operation
website copied to clipboard

'Read and write files' page issue - Testing throws error

Open crcdng opened this issue 4 years ago • 1 comments

Page URL: https://flutter.dev/docs/cookbook/persistence/reading-writing-files.html Page source: https://github.com/flutter/website/tree/master/src/docs/cookbook/persistence/reading-writing-files.md

Description of issue: The code snippet described as "To mock the method call, provide a setupAll() function in the test file. This function runs before the tests are executed." at https://flutter.dev/docs/cookbook/persistence/reading-writing-files#testing throws the following error:

Testing started at 19:05 ...

package:flutter/src/services/platform_channel.dart 142:86  MethodChannel.binaryMessenger
package:flutter/src/services/platform_channel.dart 416:5   MethodChannel.setMockMethodCallHandler
test/main_test.dart 20:10                                  main.<fn>

Null check operator used on a null value

To reproduce follow the instructions.

It would improve the quality of the Flutter documentation to make it a principle to always provide complete and tested code examples - this also applies to examples of test cases.

 flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.3 20E232 darwin-x64, locale
    en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] IntelliJ IDEA Community Edition (version 2021.1.3)
[✓] VS Code (version 1.58.1)
[✓] Connected device (3 available)

• No issues found!

Full code:

import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

// REM this does not work

void main() {
  setUpAll(() async {
    print('setup');
    final directory = await Directory.systemTemp.createTemp();
    // Mock out the MethodChannel for the path_provider plugin.
    const MethodChannel('plugins.flutter.io/path_provider')
        .setMockMethodCallHandler((MethodCall methodCall) async {
      // If you're getting the apps documents directory, return the path to the
      // temp directory on the test environment instead.
      if (methodCall.method == 'getApplicationDocumentsDirectory') {
        return directory.path;
      }
      return null;
    });
  });

  test('test', () {
    print('test');
  });
}

crcdng avatar Jul 18 '21 17:07 crcdng

Hi @i3games

This page needs an update, thanks for filing the issue!

TahaTesser avatar Jul 28 '21 13:07 TahaTesser

This page has been rewritten and the testing section was removed. Closing this issue.

atsansone avatar Jun 02 '23 15:06 atsansone