Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何使用Dio编写类构造测试?_Flutter_Mockito_Flutter Test_Dio - Fatal编程技术网

Flutter 如何使用Dio编写类构造测试?

Flutter 如何使用Dio编写类构造测试?,flutter,mockito,flutter-test,dio,Flutter,Mockito,Flutter Test,Dio,我有一个类,它将在构造函数中第一次创建时获取imgUrl。 我需要编写一个测试来确保调用Dio实例的get方法。 但是,我遇到的问题是,获取结果返回的是null而不是Future,因此我无法调用then 班级: @JsonSerializable() class DogBreed with ChangeNotifier { @JsonKey(ignore: true) final Dio dio; final String id; final String bred_for;

我有一个类,它将在构造函数中第一次创建时获取
imgUrl
。 我需要编写一个测试来确保调用
Dio
实例的
get
方法。 但是,我遇到的问题是,获取结果返回的是
null
而不是
Future
,因此我无法调用
then


班级:

@JsonSerializable()
class DogBreed with ChangeNotifier {
  @JsonKey(ignore: true)
  final Dio dio;

  final String id;
  final String bred_for;
  final String breed_group;
  final String life_span;
  final String name;
  final String origin;
  final String temperament;
  final String description;
  final Measurement height;
  final Measurement weight;

  var imgUrl = '';

  DogBreed({
    this.dio,
    this.id,
    this.bred_for,
    this.breed_group,
    this.life_span,
    this.name,
    this.origin,
    this.temperament,
    this.description,
    this.height,
    this.weight,
  }) {
    dio.get(
      'xxxxx,
      queryParameters: {
        'breed_id': id,
        'limit': 1,
      },
    ).then((result) {
      final List data = result.data;

      if (result.statusCode == 200) {
        if (data.isNotEmpty) {
          imgUrl = result.data[0]['url'];
        } else {
          imgUrl = NO_IMAGE_AVAILABLE_URL;
        }
        notifyListeners();
      }
    });
  }

  factory DogBreed.fromJson(Map<String, dynamic> json) =>
      _$DogBreedFromJson(json);
}
运行测试时的结果:

dart:core                                                           Object.noSuchMethod
package:practises/projects/dog_facts/providers/dog_breed.dart 46:7  new DogBreed
test/projects/dog_facts/providers/dog_breed_test.dart 24:32         main.<fn>

NoSuchMethodError: The method 'then' was called on null.
Receiver: null
Tried calling: then<Null>(Closure: (Response<dynamic>) => Null)
dart:core Object.noSuchMethod
套餐:实践/项目/狗狗事实/提供者/狗狗品种。dart 46:7新狗狗品种
测试/项目/狗的事实/提供者/狗的品种测试。dart 24:32主要。
NoSuchMethodError:对null调用了方法“then”。
收件人:空
尝试调用:然后(闭包:(响应)=>Null)

有谁能帮我弄清楚如何编写这个测试,或者建议我一种新的实现方法,这样我就可以在这个测试上编写一个测试了吗?

我明白了为什么,我的错误是我需要在测试中为
get
方法提供
queryParameters
。应该是:

      when(
        mockPdio.get(
          any,
          queryParameters: anyNamed('queryParameters'),
        ),
      ).thenAnswer((_) async => Response(data: 'url', statusCode: 200));

干杯

我想这回答了你的帖子:
      when(
        mockPdio.get(
          any,
          queryParameters: anyNamed('queryParameters'),
        ),
      ).thenAnswer((_) async => Response(data: 'url', statusCode: 200));