## `mockCall`

### Signature

```solidity
function mockCall(address callee, bytes calldata data, bytes calldata returnData) external;
function mockCall(address callee, uint256 msgValue, bytes calldata data, bytes calldata returnData) external;
function mockCall(address callee, bytes4 data, bytes calldata returnData) external;
function mockCall(address callee, uint256 msgValue, bytes4 data, bytes calldata returnData) external;
function mockCall(address callee, bytes calldata data, bytes calldata returnData, bool injectCode) external;
```

### Description

Mocks all calls to an address `callee` if the call data either strictly or loosely matches `data` and returns `returnData`.

When a call is made to `callee`, the call data is first checked to see if it matches in its entirety with `data`. If not, the call data is checked to see if there is a partial match, with the match starting at the first byte of the call data.

If a match is found, then `returnData` is returned from the call.

Use the `msgValue` overloads to match a specific `msg.value`. Calldata matches take precedence over `msg.value` in case of ambiguity. The `bytes4` overloads accept a function selector directly instead of requiring `abi.encodeWithSelector`.

By default, `mockCall` injects one byte of code when `callee` has no code. This allows Solidity calls without return values to pass its `extcodesize` check. Set `injectCode` to `false` to leave the account codeless.

Mocked calls are in effect until [`clearMockedCalls`](/reference/cheatcodes/clear-mocked-calls) is called.

### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `callee` | `address` | Address whose calls are mocked |
| `msgValue` | `uint256` | `msg.value` to match in value-specific overloads |
| `data` | `bytes` or `bytes4` | Calldata prefix or function selector to match |
| `returnData` | `bytes` | Data returned when the mock matches |
| `injectCode` | `bool` | Whether to add one byte of code when `callee` is codeless |

### Examples

#### Mocking an exact call

```solidity [test/MockCallExact.t.sol]
function testMockCallExact() public {
    vm.mockCall(
        address(token),
        abi.encodeWithSelector(IERC20.balanceOf.selector, alice),
        abi.encode(10 ether)
    );
    assertEq(token.balanceOf(alice), 10 ether);
}
```

#### Mocking an entire function

```solidity [test/MockCallFunction.t.sol]
function testMockCallFunction() public {
    // Mock all balanceOf calls regardless of the address parameter
    vm.mockCall(
        address(token),
        abi.encodeWithSelector(IERC20.balanceOf.selector),
        abi.encode(10 ether)
    );
    assertEq(token.balanceOf(alice), 10 ether);
    assertEq(token.balanceOf(bob), 10 ether);
}
```

#### Mocking a call with msg.value

```solidity [test/MockCallValue.t.sol]
function testMockCallValue() public {
    vm.mockCall(
        address(example),
        10 ether,
        abi.encodeWithSelector(example.pay.selector),
        abi.encode(99)
    );
    assertEq(example.pay{value: 10 ether}(1), 99);
    assertEq(example.pay{value: 1 ether}(2), 2); // Not mocked
}
```

#### Leaving an empty account codeless

```solidity [test/RecentCheatcodes.t.sol]
// [!include ~/snippets/projects/cheatcodes/test/RecentCheatcodes.t.sol:mock-without-code]
```

### Gotchas

:::warning
With `injectCode = false`, mocked calls that expect return data can succeed while the account remains codeless because Solidity checks `returndatasize`. Calls without return values and unmocked calls can still revert in the caller because of its `extcodesize` check.
:::

:::note
This cheatcode does not work on internal calls. See [issue #432](https://github.com/foundry-rs/foundry/issues/432).
:::

### Related Cheatcodes

* [`mockCalls`](/reference/cheatcodes/mock-calls) - Mock calls with different return values per invocation
* [`mockCallRevert`](/reference/cheatcodes/mock-call-revert) - Mock calls to revert
* [`clearMockedCalls`](/reference/cheatcodes/clear-mocked-calls) - Clear all mocked calls
* [`etch`](/reference/cheatcodes/etch) - Set bytecode at an address
