BloC 测试“未为类型“Object”定义方法 xxx”错误
我正在尝试学习 bloc 测试。遵循文档中所述的过程。
我有 lib/cubit/counter_cubit.dart 和 lib/cubit/counterState.dart 文件
counter_cubit.dart 是:
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
part 'counter_state.dart';
class CounterCubit extends Cubit<CounterState> {
CounterCubit() : super(CounterState(counterValue: 0, wasIncremented: false));
void increment() => emit(
CounterState(counterValue: state.counterValue + 1, wasIncremented: true));
void decrement() => emit(CounterState(
counterValue: state.counterValue - 1, wasIncremented: false));
}
counter_state.dart 是:
part of 'counter_cubit.dart';
class CounterState extends Equatable {
int counterValue;
bool wasIncremented;
CounterState({
required this.counterValue,
required this.wasIncremented,
});
@override
List<Object?> get props => [counterValue, wasIncremented];
}
而 counter_cubit_test.dart 是:
import 'package:bloc_basics/cubit/counter_cubit.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group("CounterCubit", () {
final CounterCubit counterCubit = CounterCubit();
test(
"the initial state for the CounterCubitis CounterState(counterValue:0, wasIncremented:false)",
() {
expect(counterCubit.state,
CounterState(counterValue: 0, wasIncremented: false));
});
blocTest(
"The cubit should emit a CounterState(countervalue:1, wasIncremented:true) when cubit.increment() is called.",
build: () => counterCubit,
act: (cubit) => cubit!.increment(),
expect: () => CounterState(counterValue: 1, wasIncremented: true));
});
}
该行 act: (cubit) =>
抛出 counter_cubit_test.dart
中的 cubit!.increment()The method 'increment' isn't Defined for the type 'Object'.
错误。
无法弄清楚。
I am trying to learn testing with bloc. Followed the procedure as stated in docs.
I have lib/cubit/counter_cubit.dart and lib/cubit/counterState.dart files
counter_cubit.dart is:
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
part 'counter_state.dart';
class CounterCubit extends Cubit<CounterState> {
CounterCubit() : super(CounterState(counterValue: 0, wasIncremented: false));
void increment() => emit(
CounterState(counterValue: state.counterValue + 1, wasIncremented: true));
void decrement() => emit(CounterState(
counterValue: state.counterValue - 1, wasIncremented: false));
}
counter_state.dart is:
part of 'counter_cubit.dart';
class CounterState extends Equatable {
int counterValue;
bool wasIncremented;
CounterState({
required this.counterValue,
required this.wasIncremented,
});
@override
List<Object?> get props => [counterValue, wasIncremented];
}
and the counter_cubit_test.dart is:
import 'package:bloc_basics/cubit/counter_cubit.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group("CounterCubit", () {
final CounterCubit counterCubit = CounterCubit();
test(
"the initial state for the CounterCubitis CounterState(counterValue:0, wasIncremented:false)",
() {
expect(counterCubit.state,
CounterState(counterValue: 0, wasIncremented: false));
});
blocTest(
"The cubit should emit a CounterState(countervalue:1, wasIncremented:true) when cubit.increment() is called.",
build: () => counterCubit,
act: (cubit) => cubit!.increment(),
expect: () => CounterState(counterValue: 1, wasIncremented: true));
});
}
That line act: (cubit) => cubit!.increment()
in counter_cubit_test.dart
is throwing The method 'increment' isn't defined for the type 'Object'.
Error.
Couldn't figure out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未为“Object”类型定义
方法“X”。
当特定对象的类型不清楚时通常会抛出错误,因此它会回退到通用类型Object
。根据您提供的文档,您应该在
blocTest
方法中定义 Cubit 类类型及其状态类型:The
method 'X' isn't defined for the type 'Object'.
error is usually thrown when the type of a specific object is not clear, hence it falls back to the general typeObject
.Based on the docs you provided, you should define the Cubit class type and it's state type inside
blocTest
method: