我在运行 flutter bloc 测试期间遇到问题。初始测试用例运行成功,但块测试给出了此异常。 这是我的测试代码:
import 'package:bloc_test/bloc_test.dart';
import'package:dictionary_bloc/features/dictionary_search/data/data_source/dictionary_fnx.dart';
import'package:dictionary_bloc/features/dictionary_search/presentation/bloc/dictionary_cubit.dart';
import'package:flutter_test/flutter_test.dart';
void main(){
group("cubit testing", () {
late DictionaryCubit dictionaryCubit;
setUp((){
dictionaryCubit = DictionaryCubit(DictionaryFnx());
});
test("Initial test", () {
expect(dictionaryCubit.state, NoWordSearchedState());
});
group("word search group", () {
test("number searching", (){
blocTest<DictionaryCubit,DictionaryState>("word search not successful",
build: ()=> dictionaryCubit,
act: (cubit) async => await cubit.getWordSearched("1"),
expect: ()=> <DictionaryState>[ErrorState("No WordFound")]
);
});
});
});
}
从
blocTest
中取出您的 test()
,blocTest
已经是一个测试
您不能在 blocTest 内调用 test,因为 blocTest 本身就是一个测试函数。相反,您应该直接使用 blocTest 进行测试。