Mocking and stubbing help you test your code by pretending parts of it work in a certain way. This lets you check your code without needing everything to be ready or connected.
Mocking and stubbing in Deno
import { stub } from "https://deno.land/std/testing/mock.ts"; const myStub = stub(object, "methodName", () => returnValue); // Use myStub in your test myStub.restore();
stub replaces a method on an object with a fake function.
Remember to call restore() after the test to reset the original method.
greet method to return a different greeting during the test.import { stub } from "https://deno.land/std/testing/mock.ts"; const obj = { greet: () => "Hello", }; const stubGreet = stub(obj, "greet", () => "Hi there!"); console.log(obj.greet()); // Outputs: Hi there! stubGreet.restore();
add method is stubbed to always return 42, no matter the inputs.import { stub } from "https://deno.land/std/testing/mock.ts"; const math = { add: (a, b) => a + b, }; const stubAdd = stub(math, "add", (a, b) => 42); console.log(math.add(1, 2)); // Outputs: 42 stubAdd.restore();
This program shows how to stub a method to return fake data during testing, then restore it back to the original.
import { stub } from "https://deno.land/std/testing/mock.ts"; // Original object with a method const api = { fetchData: () => { return "Real data from API"; }, }; // Stub the fetchData method to simulate API response const stubFetch = stub(api, "fetchData", () => "Mocked API data"); // Use the stubbed method console.log(api.fetchData()); // Restore the original method stubFetch.restore(); // Use the original method again console.log(api.fetchData());
Always restore stubs after tests to avoid side effects on other tests.
Stubbing helps isolate the code you want to test by controlling dependencies.
Use stubs to simulate different scenarios like errors or slow responses.
Mocking and stubbing let you replace parts of your code to test it better.
Use stub from Deno's standard library to fake methods easily.
Remember to restore original methods after testing to keep code clean.
Practice
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 BQuick Check:
Mocking purpose = Replace code parts [OK]
- Thinking mocking speeds up the whole program
- Believing mocks change production code permanently
- Confusing mocking with UI generation
stub function from Deno's standard testing library?Solution
Step 1: Recall the correct import path and syntax
Thestubfunction 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 CQuick Check:
Correct import = named from mock.ts [OK]
- Using default import instead of named import
- Wrong file path for stub
- Importing everything instead of just stub
import { stub } from "https://deno.land/std/testing/mock.ts";
const obj = {
greet() {
return "Hello";
}
};
const stubGreet = stub(obj, "greet", () => "Hi there!");
console.log(obj.greet());
stubGreet.restore();
console.log(obj.greet());Solution
Step 1: Understand stub behavior on method
The stub replacesobj.greetwith a new function returning "Hi there!" temporarily.Step 2: Analyze console outputs
Firstconsole.logcalls stubbed method, outputting "Hi there!". Afterrestore(), original method returns "Hello" on second log.Final Answer:
"Hi there!" then "Hello" -> Option DQuick Check:
Stub replaces then restores method [OK]
- Expecting original method output before restore
- Thinking stub permanently changes method
- Confusing stub with spy that tracks calls
import { stub } from "https://deno.land/std/testing/mock.ts";
const obj = {
value() {
return 42;
}
};
const stubValue = stub(obj, "val", () => 100);
console.log(obj.value());
stubValue.restore();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 AQuick Check:
Stub method must exist on object [OK]
- Using wrong method name in stub
- Forgetting to import stub
- Calling restore too early
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?Solution
Step 1: Create a stub for fetchData returning fixed value
Usestubon the object containingfetchData, replacing it with a function returning{ success: true }.Step 2: Restore original method after test
Store the stub in a variable and callrestore()after the test to revert changes.Final Answer:
Useconst stubFetch = stub(obj, "fetchData", () => ({ success: true }));then callstubFetch.restore()after test -> Option AQuick Check:
Stub with restore keeps tests clean [OK]
- Not restoring stub after test
- Replacing globally without control
- Returning wrong stub value type
Ready to prove it?
Pass the test and this pattern moves unknown
