Mocking is creating fake versions of functions or objects to simulate their behavior during tests. It helps isolate the code being tested by replacing real dependencies.
Click to reveal answer
intermediate
What is the difference between mocking and stubbing?
Stubbing provides fixed responses to function calls without tracking how they are used. Mocking can do this and also checks if functions were called correctly during tests.
Click to reveal answer
beginner
How do you create a stub in Deno's testing framework?
You can create a stub by replacing a function with a simple function that returns fixed data. For example, using a custom function or a library like Sinon for Deno.
Click to reveal answer
beginner
Why use mocking and stubbing in tests?
They help test code in isolation by controlling external dependencies. This makes tests faster, more reliable, and easier to understand.
Click to reveal answer
intermediate
What is a common tool or library for mocking in Deno?
Deno supports native mocking with its standard testing library, and third-party libraries like Sinon can also be used for advanced mocking and stubbing.
Click to reveal answer
What does stubbing mainly provide in tests?
AFixed responses to function calls
BTracking how functions are called
CRunning real external services
DAutomatically generating test data
✗ Incorrect
Stubbing replaces functions with fixed responses to control test behavior.
Which of these is a benefit of mocking in tests?
ARemoves the need for assertions
BRuns slower tests
CRequires real network calls
DIsolates code by replacing dependencies
✗ Incorrect
Mocking isolates the code under test by replacing real dependencies with fake ones.
In Deno, which library can help with mocking and stubbing?
AReact
BExpress
CSinon
DVue
✗ Incorrect
Sinon is a popular library that supports mocking and stubbing in Deno.
What is NOT a purpose of mocking?
AAutomatically fix bugs
BCheck function call counts
CReplace real implementations
DSimulate external services
✗ Incorrect
Mocking helps simulate and check behavior but does not fix bugs automatically.
Which is true about stubs compared to mocks?
AStubs track function call order
BStubs do not verify how functions are called
CStubs are used only for UI testing
DStubs run real code
✗ Incorrect
Stubs provide fixed responses but do not verify call details like mocks do.
Explain how mocking and stubbing help in testing code with external dependencies.
Think about how you avoid calling real services during tests.
You got /4 concepts.
Describe the difference between mocking and stubbing with simple examples.
Consider a fake function that just returns data vs one that also watches how it is used.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of mocking and stubbing in Deno testing?
easy
A. To speed up the execution of the entire program
B. To replace parts of code with fake versions for easier testing
C. To permanently change the behavior of functions in production
D. To automatically generate user interface components
Solution
Step 1: Understand mocking and stubbing
Mocking and stubbing are techniques used to replace parts of code with fake versions to isolate and test specific behaviors.
Step 2: Identify the purpose in testing context
They help test code without relying on real dependencies, making tests simpler and more reliable.
Final Answer:
To replace parts of code with fake versions for easier testing -> Option B
Quick Check:
Mocking purpose = Replace code parts [OK]
Hint: Mocking replaces code parts to isolate tests [OK]
Common Mistakes:
Thinking mocking speeds up the whole program
Believing mocks change production code permanently
Confusing mocking with UI generation
2. Which of the following is the correct way to import the stub function from Deno's standard testing library?
easy
A. import { stub } from "https://deno.land/std/testing/stub.ts";
B. import stub from "https://deno.land/std/testing/mock.ts";
C. import { stub } from "https://deno.land/std/testing/mock.ts";
D. import * as stub from "https://deno.land/std/testing/mock.ts";
Solution
Step 1: Recall the correct import path and syntax
The stub function is exported from "https://deno.land/std/testing/mock.ts" and must be imported using named import syntax.
Step 2: Check each option
import { stub } from "https://deno.land/std/testing/mock.ts"; uses named import from the correct path. import stub from "https://deno.land/std/testing/mock.ts"; uses default import which is incorrect. import { stub } from "https://deno.land/std/testing/stub.ts"; uses wrong file name. import * as stub from "https://deno.land/std/testing/mock.ts"; imports all as an object, not the function directly.
Final Answer:
import { stub } from "https://deno.land/std/testing/mock.ts"; -> Option C
Quick Check:
Correct import = named from mock.ts [OK]
Hint: Use named import from mock.ts for stub [OK]
Common Mistakes:
Using default import instead of named import
Wrong file path for stub
Importing everything instead of just stub
3. Given the following code snippet, what will be the output?
A. Trying to stub a non-existent method 'val' on obj
B. Missing import statement for stub
C. restore() called before stub is created
D. Incorrect arrow function syntax in stub
Solution
Step 1: Check stub target method name
The stub tries to replace method named "val" but obj only has "value" method.
Step 2: Understand stub behavior on invalid method
Stubbing a non-existent method causes an error or does not work as expected.
Final Answer:
Trying to stub a non-existent method 'val' on obj -> Option A
Quick Check:
Stub method must exist on object [OK]
Hint: Stub only existing methods on objects [OK]
Common Mistakes:
Using wrong method name in stub
Forgetting to import stub
Calling restore too early
5. You want to test a function that calls fetchData() which normally fetches data from the internet. Using Deno's stub, how can you replace fetchData to return a fixed value { success: true } only during the test, and ensure the original fetchData is restored after the test?
hard
A. Use const stubFetch = stub(obj, "fetchData", () => ({ success: true })); then call stubFetch.restore() after test
B. Replace fetchData globally without restore, returning fixed value
C. Use stub(obj, "fetchData", async () => Promise.resolve({ success: true })); but forget to restore
D. Call stub(obj, "fetchData", () => { return true; }); without storing stub
Solution
Step 1: Create a stub for fetchData returning fixed value
Use stub on the object containing fetchData, replacing it with a function returning { success: true }.
Step 2: Restore original method after test
Store the stub in a variable and call restore() after the test to revert changes.
Final Answer:
Use const stubFetch = stub(obj, "fetchData", () => ({ success: true })); then call stubFetch.restore() after test -> Option A
Quick Check:
Stub with restore keeps tests clean [OK]
Hint: Always restore stub after test to keep original code [OK]
Common Mistakes:
Not restoring stub after test
Replacing globally without control
Returning wrong stub value type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.