-
Notifications
You must be signed in to change notification settings - Fork 163
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
Enhancement: Mocking extension members #263
Comments
How does mockito prevent one from using newer language features? Can you give an example? (In your code snippet, what are |
I mean that we can't mock extension methods.
class MockFirebaseStorage extends Mock implements FirebaseStorage {} and extension FirebaseStorageX on FirebaseStorage {
Future<StorageReference> userRef() async {
// ...
}
} |
Mockito does not prevent you from using extension methods, you can mock out the behavior used by the extension instead of the entire extension. It also does not allow you to mock extension methods, because the language fundamentally does not support that. Just as with any other static methods which can't be mocked - it is your choice to use them or avoid them. Personally I don't avoid these language features and if I find myself missing the ability to mock out some bit of static functionality I consider it a design smell (overly tight coupling) or testing smell (over reliance on mocking). This issue is not actionable in this package. |
Is there a known method to do so ( Mock the behavior of extension ) because I couldn't get it to work. |
There is no method to mock extension methods. This is already documented. |
I meant the behavior used by the extension as @natebosch mentioned:
and can I apply it to an external package such as permission_handler? Thank you. |
There is no magic here - mocking the behavior that is used is what you already do with mocks. You are accustomed to mocking the behavior that is used by your code under test - that is the purpose of the mock. When I say to mock the behavior used by the extension, I mean to treat the extension that your code under test calls like any other code in the implementation. To make this explicit consider this trivial example: abstract class Foo {
int get a;
int get b;
}
extension AandB on Foo {
int get aAndB => a + b;
} You know that your code under test calls when(mockFoo.a).thenReturn(4);
when(mockFoo.b).thenReturn(6); And when your code under test reads |
Thank you @natebosch I found your answer very helpful. |
I am fully aware of the current limitations of this otherwise amazing library. For example, we cannot mock static or top-level methods. Since extensions are really just static members in disguise, Mockito doesn't support them as well.
Extension methods can simplify code a lot and I'd like to use them. However, the inability to use them with mocks forces me to create ugly wrapper classes.
The following code outputs
ERROR: Bad state: Mock method was not called within when(). Was a real method called?
and also runs the actual code inside theuserRef()
extension method.Mockito shouldn't prevent us from using newer language features.
The text was updated successfully, but these errors were encountered: