---
description: Mocks calls to an address to revert with specified data
---

## `mockCallRevert`

### Signature

```solidity
function mockCallRevert(address where, bytes calldata data, bytes calldata retdata) external;
function mockCallRevert(address where, uint256 value, bytes calldata data, bytes calldata retdata) external;
```

### Description

Reverts all calls to an address `where` if the call data either strictly or loosely matches `data` and returns `retdata` as the revert reason.

`retdata` can be a raw return message or a custom error.

When a call is made to `where`, 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 the call is reverted and `retdata` is returned.

Using the second signature, you can mock calls with a specific `msg.value`. Calldata match takes precedence over `msg.value` in case of ambiguity.

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

### Parameters

| Parameter | Type      | Description                                           |
|-----------|-----------|-------------------------------------------------------|
| `where`   | `address` | The address to mock calls to                          |
| `value`   | `uint256` | The `msg.value` to match (optional)                   |
| `data`    | `bytes`   | The calldata to match (can be partial)                |
| `retdata` | `bytes`   | The revert message or custom error to return          |

### Examples

#### Reverting with a raw error message

```solidity [test/MockCallRevert.t.sol]
function testMockCallRevert() public {
    vm.mockCallRevert(
        address(token),
        abi.encodeWithSelector(IERC20.balanceOf.selector, alice),
        "BALANCE_CHECK_FAILED"
    );
    vm.expectRevert("BALANCE_CHECK_FAILED");
    token.balanceOf(alice);
}
```

#### Reverting with a custom error

```solidity [test/MockCallRevertCustom.t.sol]
error InsufficientBalance(string message);

function testMockCallRevertCustomError() public {
    bytes memory customError = abi.encodeWithSelector(
        InsufficientBalance.selector, 
        "Not enough tokens"
    );
    vm.mockCallRevert(
        address(token),
        abi.encodeWithSelector(IERC20.transfer.selector),
        customError
    );
    vm.expectRevert(customError);
    token.transfer(bob, 100);
}
```

#### Reverting based on msg.value

```solidity [test/MockCallRevertValue.t.sol]
function testMockCallRevertValue() public {
    vm.mockCallRevert(
        address(example),
        10 ether,
        abi.encodeWithSelector(example.pay.selector),
        "INVALID_AMOUNT"
    );
    
    example.pay{value: 1 ether}(1); // Not reverted
    
    vm.expectRevert("INVALID_AMOUNT");
    example.pay{value: 10 ether}(1); // Reverted
}
```

### Gotchas

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

### Related Cheatcodes

- [`mockCall`](/reference/cheatcodes/mock-call) - Mock calls to return data
- [`mockCalls`](/reference/cheatcodes/mock-calls) - Mock calls with different return values per invocation
- [`clearMockedCalls`](/reference/cheatcodes/clear-mocked-calls) - Clear all mocked calls
