You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blockchain-development-tutorials/cadence/emulator-fork-testing/index.md
+33-21Lines changed: 33 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -291,37 +291,48 @@ This ensures the forked state is consistent across runs—essential for E2E test
291
291
292
292
:::
293
293
294
-
## Advanced: Override Specific Contracts (Optional)
294
+
## Mocking Mainnet Contracts
295
295
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`:**
297
303
298
304
```json
299
305
{
300
-
"dependencies": {
301
-
"MyModifiedContract": {
302
-
"source": "./contracts/MyModifiedContract.cdc",
303
-
"aliases": {
304
-
"mainnet": "0x1234567890abcdef",
305
-
"mainnet-fork": "0xf8d6e0586b0a20c7"// Override for testing
Copy file name to clipboardExpand all lines: docs/blockchain-development-tutorials/cadence/fork-testing/index.md
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -560,6 +560,43 @@ Configuring fork tests in the file keeps the configuration with your test code,
560
560
561
561
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.
562
562
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
+
563
600
## Pinning block heights for reproducibility
564
601
565
602
For reproducible test results, pin your tests to a specific block height:
0 commit comments