Testing with Fakes
Most unit tests should use fakes instead of making network requests to Google services.
- Code that uses
AcceleratorTypescan be tested by injecting the fakeFakeAcceleratorTypes. - Code that uses
Addressescan be tested by injecting the fakeFakeAddresses. - Code that uses
Advicecan be tested by injecting the fakeFakeAdvice. - Code that uses
Autoscalerscan be tested by injecting the fakeFakeAutoscalers. - Code that uses
BackendBucketscan be tested by injecting the fakeFakeBackendBuckets. - Code that uses
BackendServicescan be tested by injecting the fakeFakeBackendServices. - Code that uses
CrossSiteNetworkscan be tested by injecting the fakeFakeCrossSiteNetworks. - Code that uses
DiskTypescan be tested by injecting the fakeFakeDiskTypes. - Code that uses
Diskscan be tested by injecting the fakeFakeDisks. - Code that uses
ExternalVpnGatewayscan be tested by injecting the fakeFakeExternalVpnGateways. - Code that uses
FirewallPoliciescan be tested by injecting the fakeFakeFirewallPolicies. - Code that uses
Firewallscan be tested by injecting the fakeFakeFirewalls. - Code that uses
ForwardingRulescan be tested by injecting the fakeFakeForwardingRules. - Code that uses
FutureReservationscan be tested by injecting the fakeFakeFutureReservations. - Code that uses
GlobalAddressescan be tested by injecting the fakeFakeGlobalAddresses. - Code that uses
GlobalForwardingRulescan be tested by injecting the fakeFakeGlobalForwardingRules. - Code that uses
GlobalNetworkEndpointGroupscan be tested by injecting the fakeFakeGlobalNetworkEndpointGroups. - Code that uses
GlobalOperationscan be tested by injecting the fakeFakeGlobalOperations. - Code that uses
GlobalOrganizationOperationscan be tested by injecting the fakeFakeGlobalOrganizationOperations. - Code that uses
GlobalPublicDelegatedPrefixescan be tested by injecting the fakeFakeGlobalPublicDelegatedPrefixes. - Code that uses
GlobalVmExtensionPoliciescan be tested by injecting the fakeFakeGlobalVmExtensionPolicies. - Code that uses
HealthCheckscan be tested by injecting the fakeFakeHealthChecks. - Code that uses
Hostscan be tested by injecting the fakeFakeHosts. - Code that uses
ImageFamilyViewscan be tested by injecting the fakeFakeImageFamilyViews. - Code that uses
Imagescan be tested by injecting the fakeFakeImages. - Code that uses
InstanceGroupManagerResizeRequestscan be tested by injecting the fakeFakeInstanceGroupManagerResizeRequests. - Code that uses
InstanceGroupManagerscan be tested by injecting the fakeFakeInstanceGroupManagers. - Code that uses
InstanceGroupscan be tested by injecting the fakeFakeInstanceGroups. - Code that uses
InstanceSettingsServicecan be tested by injecting the fakeFakeInstanceSettingsService. - Code that uses
InstanceTemplatescan be tested by injecting the fakeFakeInstanceTemplates. - Code that uses
Instancescan be tested by injecting the fakeFakeInstances. - Code that uses
InstantSnapshotGroupscan be tested by injecting the fakeFakeInstantSnapshotGroups. - Code that uses
InstantSnapshotscan be tested by injecting the fakeFakeInstantSnapshots. - Code that uses
InterconnectAttachmentGroupscan be tested by injecting the fakeFakeInterconnectAttachmentGroups. - Code that uses
InterconnectAttachmentscan be tested by injecting the fakeFakeInterconnectAttachments. - Code that uses
InterconnectGroupscan be tested by injecting the fakeFakeInterconnectGroups. - Code that uses
InterconnectLocationscan be tested by injecting the fakeFakeInterconnectLocations. - Code that uses
InterconnectRemoteLocationscan be tested by injecting the fakeFakeInterconnectRemoteLocations. - Code that uses
Interconnectscan be tested by injecting the fakeFakeInterconnects. - Code that uses
LicenseCodescan be tested by injecting the fakeFakeLicenseCodes. - Code that uses
Licensescan be tested by injecting the fakeFakeLicenses. - Code that uses
MachineImagescan be tested by injecting the fakeFakeMachineImages. - Code that uses
MachineTypescan be tested by injecting the fakeFakeMachineTypes. - Code that uses
NetworkAttachmentscan be tested by injecting the fakeFakeNetworkAttachments. - Code that uses
NetworkEdgeSecurityServicescan be tested by injecting the fakeFakeNetworkEdgeSecurityServices. - Code that uses
NetworkEndpointGroupscan be tested by injecting the fakeFakeNetworkEndpointGroups. - Code that uses
NetworkFirewallPoliciescan be tested by injecting the fakeFakeNetworkFirewallPolicies. - Code that uses
NetworkProfilescan be tested by injecting the fakeFakeNetworkProfiles. - Code that uses
Networkscan be tested by injecting the fakeFakeNetworks. - Code that uses
NodeGroupscan be tested by injecting the fakeFakeNodeGroups. - Code that uses
NodeTemplatescan be tested by injecting the fakeFakeNodeTemplates. - Code that uses
NodeTypescan be tested by injecting the fakeFakeNodeTypes. - Code that uses
OrganizationSecurityPoliciescan be tested by injecting the fakeFakeOrganizationSecurityPolicies. - Code that uses
PacketMirroringscan be tested by injecting the fakeFakePacketMirrorings. - Code that uses
PreviewFeaturescan be tested by injecting the fakeFakePreviewFeatures. - Code that uses
Projectscan be tested by injecting the fakeFakeProjects. - Code that uses
PublicAdvertisedPrefixescan be tested by injecting the fakeFakePublicAdvertisedPrefixes. - Code that uses
PublicDelegatedPrefixescan be tested by injecting the fakeFakePublicDelegatedPrefixes. - Code that uses
RegionAutoscalerscan be tested by injecting the fakeFakeRegionAutoscalers. - Code that uses
RegionBackendBucketscan be tested by injecting the fakeFakeRegionBackendBuckets. - Code that uses
RegionBackendServicescan be tested by injecting the fakeFakeRegionBackendServices. - Code that uses
RegionCommitmentscan be tested by injecting the fakeFakeRegionCommitments. - Code that uses
RegionCompositeHealthCheckscan be tested by injecting the fakeFakeRegionCompositeHealthChecks. - Code that uses
RegionDiskTypescan be tested by injecting the fakeFakeRegionDiskTypes. - Code that uses
RegionDiskscan be tested by injecting the fakeFakeRegionDisks. - Code that uses
RegionHealthAggregationPoliciescan be tested by injecting the fakeFakeRegionHealthAggregationPolicies. - Code that uses
RegionHealthCheckServicescan be tested by injecting the fakeFakeRegionHealthCheckServices. - Code that uses
RegionHealthCheckscan be tested by injecting the fakeFakeRegionHealthChecks. - Code that uses
RegionHealthSourcescan be tested by injecting the fakeFakeRegionHealthSources. - Code that uses
RegionInstanceGroupManagerResizeRequestscan be tested by injecting the fakeFakeRegionInstanceGroupManagerResizeRequests. - Code that uses
RegionInstanceGroupManagerscan be tested by injecting the fakeFakeRegionInstanceGroupManagers. - Code that uses
RegionInstanceGroupscan be tested by injecting the fakeFakeRegionInstanceGroups. - Code that uses
RegionInstanceTemplatescan be tested by injecting the fakeFakeRegionInstanceTemplates. - Code that uses
RegionInstancescan be tested by injecting the fakeFakeRegionInstances. - Code that uses
RegionInstantSnapshotGroupscan be tested by injecting the fakeFakeRegionInstantSnapshotGroups. - Code that uses
RegionInstantSnapshotscan be tested by injecting the fakeFakeRegionInstantSnapshots. - Code that uses
RegionNetworkEndpointGroupscan be tested by injecting the fakeFakeRegionNetworkEndpointGroups. - Code that uses
RegionNetworkFirewallPoliciescan be tested by injecting the fakeFakeRegionNetworkFirewallPolicies. - Code that uses
RegionNotificationEndpointscan be tested by injecting the fakeFakeRegionNotificationEndpoints. - Code that uses
RegionOperationscan be tested by injecting the fakeFakeRegionOperations. - Code that uses
RegionSecurityPoliciescan be tested by injecting the fakeFakeRegionSecurityPolicies. - Code that uses
RegionSnapshotSettingscan be tested by injecting the fakeFakeRegionSnapshotSettings. - Code that uses
RegionSnapshotscan be tested by injecting the fakeFakeRegionSnapshots. - Code that uses
RegionSslCertificatescan be tested by injecting the fakeFakeRegionSslCertificates. - Code that uses
RegionSslPoliciescan be tested by injecting the fakeFakeRegionSslPolicies. - Code that uses
RegionTargetHttpProxiescan be tested by injecting the fakeFakeRegionTargetHttpProxies. - Code that uses
RegionTargetHttpsProxiescan be tested by injecting the fakeFakeRegionTargetHttpsProxies. - Code that uses
RegionTargetTcpProxiescan be tested by injecting the fakeFakeRegionTargetTcpProxies. - Code that uses
RegionUrlMapscan be tested by injecting the fakeFakeRegionUrlMaps. - Code that uses
RegionZonescan be tested by injecting the fakeFakeRegionZones. - Code that uses
Regionscan be tested by injecting the fakeFakeRegions. - Code that uses
ReliabilityRiskscan be tested by injecting the fakeFakeReliabilityRisks. - Code that uses
ReservationBlockscan be tested by injecting the fakeFakeReservationBlocks. - Code that uses
ReservationSlotscan be tested by injecting the fakeFakeReservationSlots. - Code that uses
ReservationSubBlockscan be tested by injecting the fakeFakeReservationSubBlocks. - Code that uses
Reservationscan be tested by injecting the fakeFakeReservations. - Code that uses
ResourcePoliciescan be tested by injecting the fakeFakeResourcePolicies. - Code that uses
RolloutPlanscan be tested by injecting the fakeFakeRolloutPlans. - Code that uses
Rolloutscan be tested by injecting the fakeFakeRollouts. - Code that uses
Routerscan be tested by injecting the fakeFakeRouters. - Code that uses
Routescan be tested by injecting the fakeFakeRoutes. - Code that uses
SecurityPoliciescan be tested by injecting the fakeFakeSecurityPolicies. - Code that uses
ServiceAttachmentscan be tested by injecting the fakeFakeServiceAttachments. - Code that uses
SnapshotSettingsServicecan be tested by injecting the fakeFakeSnapshotSettingsService. - Code that uses
Snapshotscan be tested by injecting the fakeFakeSnapshots. - Code that uses
SslCertificatescan be tested by injecting the fakeFakeSslCertificates. - Code that uses
SslPoliciescan be tested by injecting the fakeFakeSslPolicies. - Code that uses
StoragePoolTypescan be tested by injecting the fakeFakeStoragePoolTypes. - Code that uses
StoragePoolscan be tested by injecting the fakeFakeStoragePools. - Code that uses
Subnetworkscan be tested by injecting the fakeFakeSubnetworks. - Code that uses
TargetGrpcProxiescan be tested by injecting the fakeFakeTargetGrpcProxies. - Code that uses
TargetHttpProxiescan be tested by injecting the fakeFakeTargetHttpProxies. - Code that uses
TargetHttpsProxiescan be tested by injecting the fakeFakeTargetHttpsProxies. - Code that uses
TargetInstancescan be tested by injecting the fakeFakeTargetInstances. - Code that uses
TargetPoolscan be tested by injecting the fakeFakeTargetPools. - Code that uses
TargetSslProxiescan be tested by injecting the fakeFakeTargetSslProxies. - Code that uses
TargetTcpProxiescan be tested by injecting the fakeFakeTargetTcpProxies. - Code that uses
TargetVpnGatewayscan be tested by injecting the fakeFakeTargetVpnGateways. - Code that uses
UrlMapscan be tested by injecting the fakeFakeUrlMaps. - Code that uses
VpnGatewayscan be tested by injecting the fakeFakeVpnGateways. - Code that uses
VpnTunnelscan be tested by injecting the fakeFakeVpnTunnels. - Code that uses
WireGroupscan be tested by injecting the fakeFakeWireGroups. - Code that uses
ZoneOperationscan be tested by injecting the fakeFakeZoneOperations. - Code that uses
ZoneVmExtensionPoliciescan be tested by injecting the fakeFakeZoneVmExtensionPolicies. - Code that uses
Zonescan be tested by injecting the fakeFakeZones.
Import the fakes from the testing library:
import 'package:google_cloud_compute_v1/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_compute_v1/compute.dart';
import 'package:google_cloud_compute_v1/testing.dart';
final fake = FakeFirewallPolicies(
listAssociations: (request) async {
// Assert request contents here if needed.
return FirewallPoliciesListAssociationsResponse();
},
);
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_compute_v1/compute.dart';
import 'package:google_cloud_compute_v1/testing.dart';
final class MyFakeFirewallPolicies extends FakeFirewallPolicies {
@override
Future<FirewallPoliciesListAssociationsResponse> listAssociations(
ListAssociationsFirewallPolicyRequest request,
) async {
// Assert request contents here if needed.
return FirewallPoliciesListAssociationsResponse();
}
}
A Simple Test
import 'package:google_cloud_compute_v1/compute.dart';
import 'package:google_cloud_compute_v1/testing.dart';
import 'package:test/test.dart';
Future<void> functionUnderTest(FirewallPolicies service) async {
// Application logic here.
await service.listAssociations(ListAssociationsFirewallPolicyRequest());
// More application logic here.
}
void main() {
test('test', () async {
final fake = FakeFirewallPolicies(
listAssociations: (request) async {
// Assert request contents here.
return FirewallPoliciesListAssociationsResponse();
},
);
// Instead of verifying that `functionUnderTest` completes, you should verify
// the relevant properties of the result.
await expectLater(functionUnderTest(fake), completes);
});
}