I am new in TDD. I am writing a test for bloc in flutter and getting an error in the when method.
我是TDD的新手。我正在写一个测试,在颤动中的块,并得到了一个错误,当他的方法。
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:weather_clean_architecture_tdd/presentation/bloc/weather_bloc.dart';
import 'package:weather_clean_architecture_tdd/presentation/screens/weather_page.dart';
class MockWeatherBloc extends MockBloc<WeatherEvent, WeatherState>
implements WeatherBloc {}
class FakeWeatherState extends Fake implements WeatherState {}
class FakeWeatherEvent extends Fake implements WeatherEvent {}
void main() {
late MockWeatherBloc mockWeatherBloc;
setUp(() => mockWeatherBloc = MockWeatherBloc());
Widget makeTestableWidget(Widget body) {
return BlocProvider(
create: (context) => mockWeatherBloc,
child: MaterialApp(home: body),
);
}
testWidgets(
'text field trigger state to change from empty to loading',
(widgetTester) async {
// Arrange
when(() => mockWeatherBloc.state).thenReturn(() => WeatherEmpty());
// Act
await widgetTester.pumpWidget(makeTestableWidget(const WeatherPage()));
var textField = find.byType(TextField);
expect(textField, findsOneWidget);
await widgetTester.enterText(textField, 'Karachi');
await widgetTester.pump();
expect(find.text('Karachi'), findsOneWidget);
},
);
}
This is the error message I am getting
Bad state: No method stub was called from within when()
. Was a real method called, or perhaps an
extension method?
这是我得到错误状态的错误消息:没有从When()内部调用方法存根。真正的方法是调用的,还是可能是扩展的方法?
I tried importing when of mocktail but still same error is being thrown.
I am expecting the test case to pass.
我尝试导入When of moackail,但仍然抛出相同的错误。我期待着测试用例能通过。
更多回答
Should it be just when(mockWeatherBloc.state
without making it to a function call?
它是否应该只是When(mock WeatherBloc.State而不进行函数调用?
I tried that then getting error The argument type 'WeatherEmpty Function()' can't be assigned to the parameter type 'WeatherState'.
我尝试了一下,然后得到错误参数类型‘WeatherEmpty Function()’不能赋给参数类型‘WeatherState’。
The value for thenReturn
must also be changed, it can’t be a function
还必须更改ThenReturn的值,它不能是函数
When I tried when(mockWeatherBloc.state).thenReturn(WeatherEmpty());
it throws another error The following _TypeError was thrown running a test: type 'Null' is not a subtype of type 'WeatherState'
. Furthermore, I tried mocktail method when(() => mockWeatherBloc.state).thenReturn(WeatherEmpty());
, it throws the error The following ProviderNotFoundException was thrown building KeyedSubtree-[GlobalKey#ad930]: Error: Could not find the correct Provider<WeatherBloc> above this BlocBuilder<WeatherBloc,WeatherState> Widget
当我尝试when(mockWeatherBloc.state).thenReturn(WeatherEmpty());时,它抛出了另一个错误,在运行测试时抛出了以下_TypeError:类型‘Null’不是类型‘WeatherState’的子类型。此外,我尝试了MockTail方法,当(()=>mockWeatherBloc.state).thenReturn(WeatherEmpty());,它抛出以下错误:在构建KeyedSubtree时抛出ProviderNotFoundException-[GlobalKey#AD930]:错误:无法在此块生成器上找到正确的提供者
You could try
你可以试一试
when(() => mockWeatherBloc.stream)
.thenAnswer((_) => const Stream.empty());
when(() => mockWeatherBloc.state).thenAnswer((_) =>WeatherEmpty()));
when(() => mockWeatherBloc.close()).thenAnswer((_) async {});
instead of
而不是
when(() => mockWeatherBloc.state).thenReturn(() => WeatherEmpty());
更多回答
我是一名优秀的程序员,十分优秀!