-
Notifications
You must be signed in to change notification settings - Fork 1.7k
(Request) package:meta - @doNotMock #27389
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
Comments
I have to ask: Why should you care what people do in testing? Ofcourse it's only a warning, so you can ignore it if you know what you are doing. At that point, it's really just a way to give a hint that the class is so simple that you probably don't need to mock it. Has there been cases where someone mocks a class for testing, and that has caused problems? |
My first reaction is to agree with Lasse with respect to the motivation for having such a mechanism --- I'm not yet convinced that it would help anyone very much. But it would be great to have some more details about the concrete context where this idea arose, it might be useful and important in ways that we just haven't discovered yet. |
Sure, it is to prevent the following, which is real code written by Googlers: class MockDateTime extends Mock implements DateTime {}
main() {
var date = new MockDateTime();
when(date.inSeconds).thenReturn(1000);
// ...
} ... instead of just writing: main() {
var date = new DateTime(seconds: 1000 /* Or whatever the API is. */);
// ...
} There are some simple examples of classes that should not be mocked:
Another common one is RPC objects, for example: class UserProtoRequest {
String firstName;
String emailAddress;
} This shouldn't be "mocked". You can "mock" one by using "new": new UserProtoRequest()..firstName = 'Mock'..emailAddress = '[email protected]'; But users don't know that and use mockito/ |
And importantly, this would be an optional hint/warning, not an error. |
Those particular classes ( For other classes, I still don't see a problem with using mocks. It's unnecessary, but its just a test, and we have reviewers to point that out. And then, once in a blue moon, you actually do want to mock the interface, perhaps to test what happens if it breaks expected behavior (a Requiring people to add the If you think that someone is mocking I'd be much more encouraging about an analyzer feature that recognize classes that don't need to be mocked (typically simple to build and self-contained - so something where you can initialize every field and where no method accesses non-object state). If you then try to mock such a class, we could suggest that you just use the constructor to create one. |
@lrhn, why not allow mocking |
The reason to not mock So, basically, it's because it can't be turtles all the way down. We could allow mocking integers, and just throw if a non-system-integer reaches a place where we need a "real" integer, but that's not very user friendly, and it was decided that reimplementing a system integration type is probably an error. It's true that you also never need to, because you can always create instance that you need, which is what the The reason to not mock |
@lrhn Thanks for the explanation. I agree with the reasoning of When I optimize a piece of code for performance, parts of the system I consider are the CPU, memory and my knowledge of how the compiler compiles my code. When I define a class and intend it to have specific performance characteristics I rely on how the system works. As an example, I might rely on static method dispatch or inlining based on the knowledge that the compiler can optimize my code if types are monomorphic. Or I might rely on data locality knowing how my objects are laid out in memory, or whether they are allocated on the stack or the heap. If the user of my class is given the ability to unknowingly change my assumptions about how the system works, I cannot guarantee the performance characteristics that I promised to the user. The web is in a similar situation. Perhaps I'm defining a Dart class whose instances are backed by JS objects. I will be relying on the properties of Having said that, I think a |
I'd prefer sealed classes to |
Closing in favor of |
Forked from #27372.
@doNotMock
could be placed on a class definition to warn ifnoSuchMethod
is used:So, use of
mockito
(or similar), would receive a warning:cc @yjbanov @bwilkerson @eernstg @lrhn
The text was updated successfully, but these errors were encountered: