-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Internal: b/139713315
The flutter_tool currently makes heavy usage of context getter globals to inject objects throughout the tool for both configuration and testability issues. For example:
Foo get foo => context.get<Foo>();
class Bar {
void doThing() {
foo.fizz(); // context global
}
}
This is making it more difficult than necessary to unit test tooling code. Besides mocking foo, we must also ensure the context injection is correct which leads to a significant amount of boilerplate configuration for each test case. Mistakes in this boilerplate can lead to tests silently doing the wrong thing.
Previously I attempted to clean this up with the Testbed class, but this hasn't succeeded it simplifying the test cases.
Instead, we should gradually reduce our usage of these context getters which removes the need for the test configuration and makes it more explicit which instances are being used for tests. For example, rewriting the snippet above becomes:
class Bar {
Bar({Foo foo}) : _foo = foo;
final Foo _foo;
void doThing() {
_foo.fizz();
}
}
To ensure we don't regress in usage of context getters, we can use the forthcoming testWithoutContext test method that throws if context.get is invoked. See also #45739
cc @zanderso