Testing with Fakes
Most unit tests should use fakes instead of making network requests to Google services.
- Code that uses
DatasetServicecan be tested by injecting the fakeFakeDatasetService. - Code that uses
DeploymentResourcePoolServicecan be tested by injecting the fakeFakeDeploymentResourcePoolService. - Code that uses
EndpointServicecan be tested by injecting the fakeFakeEndpointService. - Code that uses
EvaluationServicecan be tested by injecting the fakeFakeEvaluationService. - Code that uses
ExampleStoreServicecan be tested by injecting the fakeFakeExampleStoreService. - Code that uses
ExtensionExecutionServicecan be tested by injecting the fakeFakeExtensionExecutionService. - Code that uses
ExtensionRegistryServicecan be tested by injecting the fakeFakeExtensionRegistryService. - Code that uses
FeatureOnlineStoreAdminServicecan be tested by injecting the fakeFakeFeatureOnlineStoreAdminService. - Code that uses
FeatureOnlineStoreServicecan be tested by injecting the fakeFakeFeatureOnlineStoreService. - Code that uses
FeatureRegistryServicecan be tested by injecting the fakeFakeFeatureRegistryService. - Code that uses
FeaturestoreOnlineServingServicecan be tested by injecting the fakeFakeFeaturestoreOnlineServingService. - Code that uses
FeaturestoreServicecan be tested by injecting the fakeFakeFeaturestoreService. - Code that uses
GenAiCacheServicecan be tested by injecting the fakeFakeGenAiCacheService. - Code that uses
GenAiTuningServicecan be tested by injecting the fakeFakeGenAiTuningService. - Code that uses
IndexEndpointServicecan be tested by injecting the fakeFakeIndexEndpointService. - Code that uses
IndexServicecan be tested by injecting the fakeFakeIndexService. - Code that uses
JobServicecan be tested by injecting the fakeFakeJobService. - Code that uses
LlmUtilityServicecan be tested by injecting the fakeFakeLlmUtilityService. - Code that uses
MatchServicecan be tested by injecting the fakeFakeMatchService. - Code that uses
MemoryBankServicecan be tested by injecting the fakeFakeMemoryBankService. - Code that uses
MetadataServicecan be tested by injecting the fakeFakeMetadataService. - Code that uses
MigrationServicecan be tested by injecting the fakeFakeMigrationService. - Code that uses
ModelGardenServicecan be tested by injecting the fakeFakeModelGardenService. - Code that uses
ModelMonitoringServicecan be tested by injecting the fakeFakeModelMonitoringService. - Code that uses
ModelServicecan be tested by injecting the fakeFakeModelService. - Code that uses
NotebookServicecan be tested by injecting the fakeFakeNotebookService. - Code that uses
OnlineEvaluatorServicecan be tested by injecting the fakeFakeOnlineEvaluatorService. - Code that uses
PersistentResourceServicecan be tested by injecting the fakeFakePersistentResourceService. - Code that uses
PipelineServicecan be tested by injecting the fakeFakePipelineService. - Code that uses
PredictionServicecan be tested by injecting the fakeFakePredictionService. - Code that uses
ReasoningEngineExecutionServicecan be tested by injecting the fakeFakeReasoningEngineExecutionService. - Code that uses
ReasoningEngineRuntimeRevisionServicecan be tested by injecting the fakeFakeReasoningEngineRuntimeRevisionService. - Code that uses
ReasoningEngineServicecan be tested by injecting the fakeFakeReasoningEngineService. - Code that uses
ScheduleServicecan be tested by injecting the fakeFakeScheduleService. - Code that uses
SessionServicecan be tested by injecting the fakeFakeSessionService. - Code that uses
SpecialistPoolServicecan be tested by injecting the fakeFakeSpecialistPoolService. - Code that uses
TensorboardServicecan be tested by injecting the fakeFakeTensorboardService. - Code that uses
VertexRagDataServicecan be tested by injecting the fakeFakeVertexRagDataService. - Code that uses
VertexRagServicecan be tested by injecting the fakeFakeVertexRagService. - Code that uses
VizierServicecan be tested by injecting the fakeFakeVizierService.
Import the fakes from the testing library:
import 'package:google_cloud_aiplatform_v1beta1/testing.dart';
Option A: Using Constructor Closures (Recommended)
You can inject behavior by passing optional function callbacks to the fake's
constructor. Methods that are not provided will throw an UnsupportedError.
import 'package:google_cloud_aiplatform_v1beta1/aiplatform.dart';
import 'package:google_cloud_aiplatform_v1beta1/testing.dart';
final fake = FakeFeatureOnlineStoreService(
generateFetchAccessToken: (request) async {
// Assert request contents here if needed.
return GenerateFetchAccessTokenResponse();
},
);
Option B: Subclassing the Fake
For more complex test setups or shared states, you can subclass the fake and
override its methods. Methods that are not overridden will throw an
UnsupportedError.
import 'package:google_cloud_aiplatform_v1beta1/aiplatform.dart';
import 'package:google_cloud_aiplatform_v1beta1/testing.dart';
final class MyFakeFeatureOnlineStoreService extends FakeFeatureOnlineStoreService {
@override
Future<GenerateFetchAccessTokenResponse> generateFetchAccessToken(
GenerateFetchAccessTokenRequest request,
) async {
// Assert request contents here if needed.
return GenerateFetchAccessTokenResponse();
}
}
A Simple Test
import 'package:google_cloud_aiplatform_v1beta1/aiplatform.dart';
import 'package:google_cloud_aiplatform_v1beta1/testing.dart';
import 'package:test/test.dart';
Future<void> functionUnderTest(FeatureOnlineStoreService service) async {
// Application logic here.
await service.generateFetchAccessToken(GenerateFetchAccessTokenRequest());
// More application logic here.
}
void main() {
test('test', () async {
final fake = FakeFeatureOnlineStoreService(
generateFetchAccessToken: (request) async {
// Assert request contents here.
return GenerateFetchAccessTokenResponse();
},
);
// Instead of verifying that `functionUnderTest` completes, you should verify
// the relevant properties of the result.
await expectLater(functionUnderTest(fake), completes);
});
}