简体   繁体   English

BloC 测试“未为类型‘对象’定义方法 xxx”错误

[英]BloC testing "The method xxx isn't defined for the type 'Object'" error

I am trying to learn testing with bloc.我正在尝试学习使用 bloc 进行测试。 Followed the procedure as stated in docs .按照文档中所述的程序进行操作。

I have lib/cubit/counter_cubit.dart and lib/cubit/counterState.dart files我有 lib/cubit/counter_cubit.dart 和 lib/cubit/counterState.dart 文件

counter_cubit.dart is: 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 is: 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];
}

and the counter_cubit_test.dart is: 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));
  });
}

That line act: (cubit) => cubit..increment() in counter_cubit_test.dart is throwing The method 'increment' isn't defined for the type 'Object'.该行行为: counter_cubit_test.dart中的act: (cubit) => cubit..increment()正在抛出The method 'increment' isn't defined for the type 'Object'. Error.错误。

Couldn't figure out.想不通。

The method 'X' isn't defined for the type 'Object'. 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 type Object .当特定 object 的类型不明确时,通常会抛出错误,因此它会退回到一般类型Object

Based on the docs you provided, you should define the Cubit class type and it's state type inside blocTest method:根据您提供的文档,您应该在blocTest方法中定义 Cubit class 类型和 state 类型:

blocTest<CounterCubit, CounterState>( // <-- Notice the defined types
  "The cubit should emit a CounterState(countervalue:1, wasIncremented:true) when cubit.increment() is called.",
  build: () => CounterCubit(), // <-- Create an instance of a Cubit
  act: (cubit) => cubit.increment(), // <-- ! is not needed here since the type is defined
  expect: () => [ // <-- "expect" should be defined as an array of expected states
    CounterState(counterValue: 1, wasIncremented: true),
  ]
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM