Skip to content

docs(storage): user guide for mocking Storage#3483

Merged
dbolduc merged 3 commits intogoogleapis:mainfrom
dbolduc:storage-mocking-user-guide
Oct 9, 2025
Merged

docs(storage): user guide for mocking Storage#3483
dbolduc merged 3 commits intogoogleapis:mainfrom
dbolduc:storage-mocking-user-guide

Conversation

@dbolduc
Copy link
Copy Markdown
Member

@dbolduc dbolduc commented Oct 7, 2025

@product-auto-label product-auto-label bot added the api: storage Issues related to the Cloud Storage API. label Oct 7, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Oct 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.86%. Comparing base (a1afde2) to head (f9b01b1).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3483      +/-   ##
==========================================
+ Coverage   95.84%   95.86%   +0.02%     
==========================================
  Files         120      120              
  Lines        4789     4789              
==========================================
+ Hits         4590     4591       +1     
+ Misses        199      198       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbolduc dbolduc force-pushed the storage-mocking-user-guide branch from 2a3e083 to 03a10cf Compare October 8, 2025 15:08
@dbolduc dbolduc marked this pull request as ready for review October 8, 2025 15:31
@dbolduc dbolduc requested a review from a team October 8, 2025 15:31
Comment on lines +27 to +31
- Why the design of the `Storage` client deviates from the design of other
Google Cloud clients
- How to write testable interfaces using the `Storage` client
- How to mock reads
- How to mock writes
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the why is important, but tutorials / guides should emphasize how to do things... Why is for "in-depth guides":

Suggested change
- Why the design of the `Storage` client deviates from the design of other
Google Cloud clients
- How to write testable interfaces using the `Storage` client
- How to mock reads
- How to mock writes
- How to write testable interfaces using the `Storage` client
- How to mock reads
- How to mock writes
- Why the design of the `Storage` client deviates from the design of other
Google Cloud clients

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

mocking guide (which applies to the `StorageControl` client), see
[How to write tests using a client](../mock_a_client.md).

## Design rationale
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider moving this to the end or even a separate page.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

forward requests from the client to their stub (which could be the real
implementation or a mock).

Because dynamic dispatch is used, the exact type of the stub does not need to be
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Because dynamic dispatch is used, the exact type of the stub does not need to be
Because these clients use dynamic dispatch, the exact type of the stub does not need to be

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

implementation or a mock).

Because dynamic dispatch is used, the exact type of the stub does not need to be
known by the compiler. The clients do not need to be templated on their stub.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"templated" is a C++-ism, "generic on"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment on lines +75 to +76
Applications that need to test their code, should write their interfaces in
terms of the generic `T`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Applications that need to test their code, should write their interfaces in
terms of the generic `T`.
Applications that need to test their code, should write their interfaces in
terms of the generic `T` with the correct constraints.

or maybe "constrained as needed"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


## Mocking reads

Let's say we have an application function which downloads an object and counts
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the style guide. Google docs use the second person singular... "In this guide you will test an application function ..."

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used second person, singular.

I like saying "we" because it makes me feel like I am on the same team as the reader. It's fine though.

Comment on lines +91 to +92
We want to test our code against a known response from the server. We can do
this by faking the `ReadObjectResponse`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too, change to use "you".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

{{#rustdoc_include ../../samples/tests/storage/mocking.rs:fake-read-object-resp}}
```

We define the mock as usual, using `mockall`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure "as usual" is quite right. It is usual for us in this project, applications may use something else. Maybe "This guide uses the mockall crate to create a mock. You can use a different crate in your tests" ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. FWIW, I think mockall is the main mocking framework in the ecosystem.

@dbolduc dbolduc force-pushed the storage-mocking-user-guide branch from 03a10cf to f9b01b1 Compare October 8, 2025 20:01
Copy link
Copy Markdown
Member Author

@dbolduc dbolduc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL

Comment on lines +27 to +31
- Why the design of the `Storage` client deviates from the design of other
Google Cloud clients
- How to write testable interfaces using the `Storage` client
- How to mock reads
- How to mock writes
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

mocking guide (which applies to the `StorageControl` client), see
[How to write tests using a client](../mock_a_client.md).

## Design rationale
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

forward requests from the client to their stub (which could be the real
implementation or a mock).

Because dynamic dispatch is used, the exact type of the stub does not need to be
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

implementation or a mock).

Because dynamic dispatch is used, the exact type of the stub does not need to be
known by the compiler. The clients do not need to be templated on their stub.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment on lines +75 to +76
Applications that need to test their code, should write their interfaces in
terms of the generic `T`.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


## Mocking reads

Let's say we have an application function which downloads an object and counts
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used second person, singular.

I like saying "we" because it makes me feel like I am on the same team as the reader. It's fine though.

Comment on lines +91 to +92
We want to test our code against a known response from the server. We can do
this by faking the `ReadObjectResponse`.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

{{#rustdoc_include ../../samples/tests/storage/mocking.rs:fake-read-object-resp}}
```

We define the mock as usual, using `mockall`.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. FWIW, I think mockall is the main mocking framework in the ecosystem.

@dbolduc dbolduc merged commit c341a0e into googleapis:main Oct 9, 2025
24 checks passed
@dbolduc dbolduc deleted the storage-mocking-user-guide branch October 10, 2025 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mocking storage samples + explain rationale

2 participants