我尝试用我的 tdd 项目对 flutter 进行测试,但出现了这些错误
错误状态:没有从 `when()` 中调用任何方法存根。是调用了真正的方法,还是扩展方法?
abstract class HomeRepository {
Future<Either<Failure,BaseModel>> testCase({required ApiRequest apiRequest});
}
class TestCase {
HomeRepository homeRepository;
TestCase({required this.homeRepository});
Future<Either<Failure,BaseModel>>call({required ApiRequest apiRequest})async{
return await homeRepository.testCase(apiRequest: apiRequest);
}
}
### 现在这是我的测试页面
class HomeTest extends Mock implements HomeRepository{
@override
Future<Either<Failure,BaseModel>> testCase({required ApiRequest apiRequest})async{
return await Right(AppModel.otpVerify);
}
}
void main(){
ApiRequest apiRequest = ApiRequest(method: ApiMethod.post, endPoint: '/');
late HomeTest homeTest;
late TestCase testCase;
setUp(() {
homeTest = HomeTest();
testCase = TestCase(homeRepository: homeTest);
});
test('testcase', () async {
// Setup
when(homeTest.testCase(apiRequest: apiRequest)).thenAnswer((_) async => right(AppModel.otpVerify));
// Action
final result = await testCase(apiRequest: apiRequest);
// Verification
expect(result, right(AppModel.otpVerify)); // Ensure the expected result matches the actual result
});
}
我正在尝试正确运行测试
我找到了灵魂:) 只需替换这个导入即可
import 'package:mockito/mockito.dart';
到此
import 'package:mocktail/mocktail.dart';
**所以测试我纠正了一些错误,这是新的**
class MockAuthRepo extends Mock implements AuthRepositories {
@override
Future<Either<Failure, BaseModel>> testCase({required ApiRequest apiRequest}) async {
return await Future.value(right(AppModel.otpVerify));
}
}
void main() {
late MockAuthRepo mockAuthRepo;
late PhoneVerifyUseCase useCase;
OtpPageModel otpPageModel = OtpPageModel(phoneNumber: '0939048067');
ApiRequest apiRequest = ApiRequest(
method: ApiMethod.post,
endPoint: EndPoints.login,
model: AppModel.loginModel,
body: otpPageModel.toJson(),
);
setUp(() {
mockAuthRepo = MockAuthRepo();
useCase= PhoneVerifyUseCase(authRepositories: mockAuthRepo);
});
test('should get trivia for the number from the repository', () async{
// Setup
when(() => mockAuthRepo.phoneVerify(apiRequest: apiRequest))
.thenAnswer((_) async => right(AppModel.loginModel));
// Action
final result = await useCase(apiRequest: apiRequest);
// Verification
expect(result, Right(AppModel.loginModel));
});
}
回购协议是
abstract class AuthRepositories{
Future<Either<Failure,BaseModel>> phoneVerify({required ApiRequest apiRequest});
Future<Either<Failure,BaseModel>> otpVerify({required ApiRequest apiRequest});
Future<Either<Failure,BaseModel>> getAppOptions({required ApiRequest apiRequest});
}
用例是 `
class PhoneVerifyUseCase{
AuthRepositories authRepositories ;
PhoneVerifyUseCase({required this.authRepositories});
Future<Either<Failure,BaseModel>>call({required ApiRequest apiRequest})async{
return await authRepositories.phoneVerify(apiRequest: apiRequest);
}
}`