Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
anas2027 opened this issue Apr 8, 2024 · 2 comments

Comments

@anas2027
Copy link

anas2027 commented Apr 8, 2024

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
Copy link
Author

anas2027 commented Apr 8, 2024

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
Copy link
Owner

How is this related to the flutter_native_splash package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants