MissingDummyValueError: Either<Failure, Class> 这意味着 Mockito 不够智能,无法生成“Either<Failure, Class>”类型的虚拟值

问题描述 投票:0回答:1

我正在尝试使用 Either 包编写测试,但出现以下错误。错误中提到的provideDummy是什么?

MissingDummyValueError: Either<Failure, Weather>
This means Mockito was not smart enough to generate a dummy value of type
'Either<Failure, Weather>'. Please consider using either 'provideDummy' or 
'provideDummyBuilder'
functions to give Mockito a proper dummy value.

Please note that due to implementation details Mockito sometimes needs users
to provide dummy values for some types, even if they plan to explicitly stub
all the called methods.

package:mockito/src/dummies.dart 156:3                             dummyValue
test/helpers/test_helper.mocks.dart 72:17                          
MockWeatherRepository.getCurrentWeather
test/domain/use_cases/get_current_weather_usecase_test.dart 31:29  main.<fn>

我该如何解决?

flutter dart testing mockito
1个回答
0
投票

我不久前遇到了这个问题,并尝试了

provideDummy
,但由于有关它的文档较少,我什么也没得到,然后迁移到
mocktail
包,它通常更容易(尽管API相同)并且无需代码生成即可处理模拟,并且最重要的是处理你的情况,只需你可以像这样模拟
MyClass

import 'package:mocktail/mocktail.dart';

class MockMyClass extends Mock implements MyClass {}

您必须进行的唯一编辑是在存根时不要直接调用MockClass,您调用从模拟类中调用存根方法的函数,如下所示:

// Act
// Instead of 
when(MockMyClass.fetchAllFromCloud())
            .thenAnswer((_) async => <ItemModel>[]);

// Do this () => Mock.method
when(() => MockMyClass.fetchAllFromCloud())
            .thenAnswer((_) async => <ItemModel>[]);

您还应该检查它的文档这里

© www.soinside.com 2019 - 2024. All rights reserved.