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

Mock with default stub implementation feature #233

Closed
dfdgsdfg opened this issue Apr 1, 2024 · 3 comments
Closed

Mock with default stub implementation feature #233

dfdgsdfg opened this issue Apr 1, 2024 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@dfdgsdfg
Copy link

dfdgsdfg commented Apr 1, 2024

How about add default stub when define the mock class?

class Cat {
  final Future<String> meow() => repo.getMeow;
}

class MockCat extends Mock implements Cat {
  // Default stub
  final Future<String> meow() => Future.value('meow');
}

final cat = MockCat();

cat.meow(); // return default stub 'meow' from RealCall

when(() => cat.meow()).thenAnswer((_) async => 'grrrrrrr'); // override RealCall

cat.meow(); // return override stub 'grrrrrrr'

Is this possible by any chance?

Work around

class MockCat extends Mock implements Cat {
  McokCat() {
    // as default stub. seems to working well
    // but cat.reset() remove default stub setting below
    when(() => meow()).thenAnswer((_) async => 'meow');
  }
}
@felangel
Copy link
Owner

felangel commented Apr 2, 2024

If you want to have defaults then I recommend creating a Fake:

import 'package:mocktail/mocktail.dart';

class Cat {
  Future<String> meow() async => 'meow';
}

class FakeCat extends Fake implements Cat {
  @override
  Future<String> meow() async => 'default meow';
}

void main() async {
  final cat = FakeCat();

  print(await cat.meow()); // default meow
}

Hope that helps! Closing for now 👍

@felangel felangel closed this as completed Apr 2, 2024
@felangel felangel added the question Further information is requested label Apr 2, 2024
@felangel felangel self-assigned this Apr 2, 2024
@xiprox
Copy link

xiprox commented Apr 23, 2024

Hi. From what I can tell, it is not possible to later override the behavior of a Fake with when. I'm wondering if I could define a Fake/Mock class with default behavior, then override some of that only where necessary. For example, a repo that returns empty data unless overriden.

@felangel
Copy link
Owner

Hi. From what I can tell, it is not possible to later override the behavior of a Fake with when. I'm wondering if I could define a Fake/Mock class with default behavior, then override some of that only where necessary. For example, a repo that returns empty data unless overriden.

If you really want to do that then you can add the stubs in the mock class’s constructor body or create a helper function that constructs the mock instance and stubs the default methods.

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

No branches or pull requests

3 participants