Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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 省道/颤振测试立方体,比较预期和实际的问题_Flutter_Dart_Bloc_Bloc Test - Fatal编程技术网

Flutter 省道/颤振测试立方体,比较预期和实际的问题

Flutter 省道/颤振测试立方体,比较预期和实际的问题,flutter,dart,bloc,bloc-test,Flutter,Dart,Bloc,Bloc Test,我正在使用WeatherAPI开发一个应用程序。我目前无法实现一些工作测试。我试图遵循ResoCoders指南,并实际实现了所有状态、我使用Cubit替代的集团、类、函数。。。几乎一样 这是我的测试代码: blocTest<WeatherCubit, WeatherBaseState>( 'Cubit emits WeatherLoaded', build: () { return WeatherCubit(weatherRepository:

我正在使用WeatherAPI开发一个应用程序。我目前无法实现一些工作测试。我试图遵循ResoCoders指南,并实际实现了所有状态、我使用Cubit替代的集团、类、函数。。。几乎一样

这是我的测试代码:

blocTest<WeatherCubit, WeatherBaseState>(
      'Cubit emits WeatherLoaded',
      build: () {
        return WeatherCubit(weatherRepository: mockWeatherRepository);
      },
      act: (WeatherCubit cubit) => cubit.getWeather(),
      expect: () => [
        WeatherLoaded(
            temperature: temperature,
            ...
            lat: lat,
            lon: lon)
      ],
    );
这是我在调试控制台上的错误信息:

Expected: [Instance of 'WeatherLoaded']
  Actual: [Instance of 'WeatherLoaded']
   Which: at location [0] is <Instance of 'WeatherLoaded'> instead of <Instance of 'WeatherLoaded'>

WARNING: Please ensure state instances extend Equatable, override == and hashCode, or implement Comparable.
Alternatively, consider using Matchers in the expect of the blocTest rather than concrete state instances.
我试着用火柴,但不太懂怎么用

如果问题出在这里,我对WeatherCubit的实现:

class WeatherCubit extends Cubit<WeatherBaseState> {
  final IWeatherRepository weatherRepository; //IWeatherRepository is interface

  WeatherCubit({required this.weatherRepository})
      : super(LoadingWeather()); // I use LoadingWeather as initial state


  Future<void> getWeather() async {
    final Position location = await weatherRepository.determinePosition();
    final WeatherData data = await weatherRepository.getWeather(
        lat: location.latitude, 
        lon: location.longitude); 
    final WeatherLoaded weatherLoaded = WeatherLoaded(
        temperature: data.temperature,
        ...
        lat: data.lat, 
        lon: data.lon); 
    emit(weatherLoaded); 
  }
}
如果要测试类型,可以使用matcher

expect: () => [isA<WeatherLoaded>()];

如果您试图比较返回对象的值,则需要使用包,或者手动覆盖Cubit中的hashCode和==运算符。

谢谢!实际上,WeatherLoaded实现了与ResoCoder示例类似的Equalable。但是我需要改变什么呢?我想测试是否正确创建了状态。扩展Equatable时,必须重写props getter并将所有类变量放入列表中@覆盖列表获取道具=>[纬度、经度、温度]```它由Equatable自动生成,所以我已经有了它。这不可能是问题所在@覆盖列表获取道具=>[lat,lon,temperature,…]@覆盖布尔?获取stringify=>true;