0

I want to use JSON files to mock HTTP responses in my Flutter tests. On gitlab CI the tests are passing. Locally also. But on codemagic.io there is an error:

FileSystemException: Cannot open file, path = 'test_resources/mock_response.json' (OS Error: No such file or directory, errno = 2)

Mikhail Tokarev
  • 2,843
  • 1
  • 14
  • 35
jarekbutek
  • 585
  • 6
  • 13

1 Answers1

1

This is related to `flutter test` sets current directory differently depending on how test was executed. Solution provided by apaatsio in the first comment worked for me.

So for example you can have something like this:

import 'dart:io';
import 'package:path/path.dart';

String loadResource(String name) => File("$_testDirectory/test_resources/$name").readAsStringSync();

// From https://github.com/flutter/flutter/issues/20907#issuecomment-466185328
final _testDirectory = join(
  Directory.current.path,
  Directory.current.path.endsWith('test') ? '' : 'test',
);

And then use it:

final mockResponse = loadResource('mock_response.json');
janstol
  • 2,857
  • 1
  • 18
  • 21