def replace_key_value(lookup, new_value, expected_dict):
    """
    Replaces the value matching the key 'lookup' in the 'expected_dict' with
    the new value 'new_value'.
    """
    for key, value in expected_dict.items():
        if lookup == key:
            if isinstance(value, dict) and isinstance(new_value, dict):
                value.update(new_value)
            else:
                expected_dict[key] = new_value
        elif isinstance(value, dict):
            expected_dict[key] = replace_key_value(lookup, new_value, value)
    return expected_dict