Google Professional Cloud Developer Question 276
Select 3Google Cloud PlatformYou are developing a Google Cloud Function that processes user data and writes results to a Cloud Firestore database. You want to implement unit tests to ensure the function logic works as expected. How should you design your unit tests?
- A
Mock external dependencies, such as the Firestore client, to isolate the function logic.
- B
Deploy the Cloud Function to the testing environment and test it against a real Firestore database.
- C
Use a testing framework like Mocha or Jest to run the unit tests locally.
- D
Include actual network calls to Firestore to validate your integration with the database.
- E
Test edge cases and invalid inputs to ensure your function handles them correctly.
Show answer and explanation
Correct answers: A, C, E
Explanation
Unit tests aim to validate the logic of your function in isolation, without involving external systems like databases or network calls. Mocking dependencies (e.g., Firestore client) and using testing frameworks like Mocha or Jest are essential practices for effective unit testing. Additionally, testing edge cases and invalid inputs ensures the resilience of your function. Integration or end-to-end tests are better suited for testing interactions with external systems.
- A. Correct.
Correct. Mocking external dependencies like Firestore ensures the unit test focuses solely on the function logic, isolating it from external systems.
- B. Incorrect.
Incorrect. Deploying the function and testing against a real Firestore instance would be considered an integration test, not a unit test.
- C. Correct.
Correct. Testing frameworks like Mocha or Jest are commonly used to run unit tests locally, providing tools for assertions and mocking.
- D. Incorrect.
Incorrect. Including actual network calls would turn the test into an integration or end-to-end test, which is not the goal of unit testing.
- E. Correct.
Correct. Unit tests should validate how the function handles edge cases and invalid inputs to ensure robust logic.