overrideWithValue only makes test harder than before #793
-
Hi, I am trying to update to the latest 1.0-dev, and have some questions about removing Pattern 1: Get provider value lazilyBefore: void main() {
late FakeSharedPreferences fakeSharedPreferences;
late ProviderContainer container;
setUp(() {
fakeSharedPreferences = FakeSharedPreferences();
container = ProviderContainer(
overrides: [
sharedPreferencesDataSourceProvider.overrideWithValue(
fakeSharedPreferences,
),
],
);
});
test('case 1', () {
// setup data before read usecase
fakeSharedPreferences.map = {
'key': false,
};
final useCase = container.read(myUseCaseProvider);
// test
}
test('case 2', () {
fakeSharedPreferences.map = {
'key': true,
};
final useCase = container.read(myUseCaseProvider);
// test
}
} After: void main() {
late FakeSharedPreferences fakeSharedPreferences;
ProviderContainer createContainer() {
return ProviderContainer(
overrides: [
sharedPreferencesDataSourceProvider.overrideWithProvider(
Provider<SharedPreferences>(
(_) => fakeSharedPreferences,
),
),
],
);
}
setUp(() {
fakeSharedPreferences = FakeSharedPreferences();
});
test('case 1', () {
fakeSharedPreferences.map = {
'key': false,
};
final container = createContainer();
final useCase = container.read(myUseCaseProvider);
// test
}
test('case 2', () {
fakeSharedPreferences.map = {
'key': true,
};
final container = createContainer();
final useCase = container.read(myUseCaseProvider);
// test
}
} Pattern 2: Override all family providerBefore: void main() {
late FakeObj fakeObj;
late ProviderContainer container;
setUp(() {
fakeObj = FakeObj();
container = ProviderContainer(
overrides: [
sharedPreferencesDataSourceProvider.overrideWithProvider(
(_) => Provider(
(ref) => fakeObj,
),
),
],
);
});
} After: void main() {
late FakeObj fakeObj;
ProviderContainer container(Param param) {
return ProviderContainer(
overrides: [
sharedPreferencesDataSourceProvider(param).overrideWithValue(
fakeObj,
),
],
);
}
setUp(() {
fakeObj = FakeObj();
});
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
To begin with, I'd advise against Your snippets are a bit broad. Could you explain in depth what the problem is and why both use-cases are desirable? |
Beta Was this translation helpful? Give feedback.
To begin with, I'd advise against
setUp
for creatingProviderContainer
& override. That should be part of the test, not a setUp function.Your snippets are a bit broad. Could you explain in depth what the problem is and why both use-cases are desirable?