jonbhanson / flutter_native_splash

Automatically generates native code for adding splash screens in Android and iOS. Customize with specific platform, background color and splash image.
https://pub.dev/packages/flutter_native_splash
MIT License
1.28k stars 200 forks source link

im trying test my app im started with useCase Casue im build my app with Tdd Architecture im getting these error #686

Open anas2027 opened 1 month ago

anas2027 commented 1 month ago

Bad state: No method stub was called from within when(). Was a real method called, or perhaps an extension method?

here my repo


abstract class HomeRepository {

    Future<Either<Failure,BaseModel>> testCase({required ApiRequest apiRequest});
}

here my usecase

class TestCase {
  HomeRepository homeRepository;
  TestCase({required this.homeRepository});
  Future<Either<Failure,BaseModel>>call({required ApiRequest apiRequest})async{
    return await homeRepository.testCase(apiRequest: apiRequest);
  }
}

### now here is my test page

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
  });

}
anas2027 commented 1 month ago

i find a the soultion :) just replace this import import 'package:mockito/mockito.dart'; to this import 'package:mocktail/mocktail.dart';

so the testing i correct some error this is new one

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));

  });

}

and the repo is


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});

}

and the use case is ` class PhoneVerifyUseCase{ AuthRepositories authRepositories ; PhoneVerifyUseCase({required this.authRepositories});

Future<Either<Failure,BaseModel>>call({required ApiRequest apiRequest})async{ return await authRepositories.phoneVerify(apiRequest: apiRequest); }

}`

jonbhanson commented 1 month ago

How is this related to the flutter_native_splash package?