Skip to content

Commit 348fbb8

Browse files
committed
update mocking docs
1 parent fe12ab9 commit 348fbb8

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

docs/blockchain-development-tutorials/cadence/emulator-fork-testing/index.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -291,37 +291,48 @@ This ensures the forked state is consistent across runs—essential for E2E test
291291

292292
:::
293293

294-
## Advanced: Override Specific Contracts (Optional)
294+
## Mocking Mainnet Contracts
295295

296-
If you need to test against a modified version of a contract, you can override specific contracts while inheriting others:
296+
Just like mocking dependencies in unit tests, you can **mock real mainnet contracts** by deploying modified versions—perfect for testing upgrades, bug fixes, or alternative implementations against real production state.
297+
298+
Configure the mock in `flow.json`, then deploy to the forked emulator. Your mock takes precedence while other contracts use real mainnet versions.
299+
300+
### Example
301+
302+
**1. Configure in `flow.json`:**
297303

298304
```json
299305
{
300-
"dependencies": {
301-
"MyModifiedContract": {
302-
"source": "./contracts/MyModifiedContract.cdc",
303-
"aliases": {
304-
"mainnet": "0x1234567890abcdef",
305-
"mainnet-fork": "0xf8d6e0586b0a20c7" // Override for testing
306-
}
307-
},
306+
"accounts": {
307+
"flow-token-mainnet": {
308+
"address": "0x1654653399040a61",
309+
"key": "0000000000000000000000000000000000000000000000000000000000000000"
310+
}
311+
},
312+
"contracts": {
308313
"FlowToken": {
314+
"source": "./contracts/FlowTokenModified.cdc",
309315
"aliases": {
310316
"mainnet": "0x1654653399040a61"
311-
// Still inherits mainnet address on fork
312317
}
313318
}
319+
},
320+
"deployments": {
321+
"mainnet-fork": {
322+
"flow-token-mainnet": ["FlowToken"]
323+
}
314324
}
315325
}
316326
```
317327

318-
This is useful for:
328+
**2. Deploy the mock:**
319329

320-
- Testing a modified/upgraded version of a contract
321-
- Using mock contracts for specific scenarios
322-
- Pointing to staging deployments
330+
```bash
331+
flow emulator --fork mainnet
332+
flow project deploy --network mainnet-fork --update
333+
```
323334

324-
For most use cases, inheritance alone is sufficient.
335+
Your dapp now uses the mocked FlowToken while FungibleToken, USDC, and all other contracts use real mainnet versions.
325336

326337
## Install Dependencies
327338

@@ -729,12 +740,13 @@ Use the same approach with Playwright, Puppeteer, or any browser automation tool
729740
730741
### Testing Contract Upgrades
731742
732-
Test a contract upgrade against real mainnet state:
743+
Test a contract upgrade against real mainnet state by mocking the contract with your upgraded version:
733744
734-
1. Start forked emulator
735-
2. Deploy your upgraded contract to the test environment
736-
3. Run scripts/transactions that interact with both old mainnet contracts and your new contract
737-
4. Verify behavior with real user account states
745+
1. Configure the mock in `flow.json` (see [Mocking Mainnet Contracts](#mocking-mainnet-contracts))
746+
2. Start the forked emulator
747+
3. Deploy your upgraded contract: `flow project deploy --network mainnet-fork --update`
748+
4. Test your dapp against the upgraded contract with all real mainnet state intact
749+
5. Verify existing integrations and users aren't broken by the upgrade
738750
739751
### Debugging User-Reported Issues
740752

docs/blockchain-development-tutorials/cadence/fork-testing/index.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,43 @@ Configuring fork tests in the file keeps the configuration with your test code,
560560

561561
You can also run specific test files or change the network/block height in the pragma as needed. See the [Fork Testing Flags] reference for more options.
562562

563+
## Mocking Mainnet Contracts in Tests
564+
565+
Just like mocking dependencies in unit tests, you can **mock real mainnet contracts** by deploying modified versions—perfect for testing upgrades, bug fixes, or alternative implementations against real production state.
566+
567+
Use `Test.deployContract()` to deploy your mock to any mainnet account address. Your mock takes precedence while other contracts continue using real mainnet versions.
568+
569+
### Example
570+
571+
```cadence
572+
#test_fork(network: "mainnet", height: nil)
573+
574+
import Test
575+
576+
access(all) fun setup() {
577+
// Deploy mock FlowToken to the real mainnet address
578+
let err = Test.deployContract(
579+
name: "FlowToken",
580+
path: "../contracts/FlowTokenModified.cdc",
581+
arguments: []
582+
)
583+
Test.expect(err, Test.beNil())
584+
}
585+
586+
access(all) fun testMockedFlowToken() {
587+
// Test now uses mocked FlowToken
588+
// All other contracts (FungibleToken, USDC, etc.) use real mainnet versions
589+
590+
let scriptResult = Test.executeScript(
591+
Test.readFile("../scripts/CheckBalance.cdc"),
592+
[Address(0x1654653399040a61)]
593+
)
594+
Test.expect(scriptResult, Test.beSucceeded())
595+
}
596+
```
597+
598+
This validates your contract changes against real production state and integrations.
599+
563600
## Pinning block heights for reproducibility
564601

565602
For reproducible test results, pin your tests to a specific block height:

docs/build/cadence/smart-contracts/testing-strategy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ See also: [Fork Testing with Cadence], [Fork Testing Flags].
9292

9393
function App() {
9494
return (
95-
<FlowProvider
96-
config={{
95+
<FlowProvider
96+
config={{
9797
accessNodeUrl: 'http://localhost:8888',
9898
flowNetwork: 'mainnet-fork', // Uses fork network with inherited aliases
9999
}}

0 commit comments

Comments
 (0)