Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve display of failures for assertDatabaseHas #43736

Merged
merged 1 commit into from
Aug 17, 2022

Conversation

pkracer
Copy link
Contributor

@pkracer pkracer commented Aug 17, 2022

This PR attempts to improve the developer experience when debugging assertDatabaseHas test failures.

Currently, when a assertDatabaseHas assertion has failed, similar results may be displayed. The current functionality for assertDatabaseHas is helpful in many scenarios but in others, it can make it difficult to track down the cause the failure.

For example, consider the following test assertion:

// test code that updates some database table

$this-assertDatabaseHas('some_table',  [
    'column_a' => 'expected_column_a_value',
    'column_e' => 'expected_column_e_value',
    'column_h' => 'expected_column_h_value',
]);

When a test failure occurs and similar results are displayed with the failure message, all of the table columns are shown even though, in this case, we were concerned with 3 columns. For example:

Failed asserting that a row in the [some_table] matches the attributes {
    'column_a': 'expected_column_a_value',
    'column_h': 'expected_column_h_value',
    'column_e': 'expected_column_e_value',
}

Found similar results: [
    {
        'column_a': 'column_a_value',
        'column_b': 'column_b_value',
        'column_c': 'column_c_value',
        'column_d': 'column_d_value',
        'column_e': 'column_e_value',
        'column_f': 'column_f_value',
        'column_g': 'column_g_value',
        'column_h': 'column_h_value',
        'column_i': 'column_i_value',
        'column_j': 'column_j_value',
        'column_k': 'column_k_value',
        ...
    }
]

It can be time consuming to locate each column and compare it to the expectation. Also, when the assertion columns/values are in a different order than the table schema (like in the example above), it makes it that much more complex to find the cause of the failure. The more columns a table has, the more complex it is to track down failures and the less likely a developer is going to pass columns in to their assertions in the same order as the table schema.

The PR includes updates the "similar results" functionality so that only the columns that are being asserted on get displayed back as well as displaying them in the exact order they have been asserted on.

Using the example from above, but including with the changes in this PR make the comparison much simpilar:

Failed asserting that a row in the [some_table] matches the attributes {
    'column_a': 'expected_column_a_value',
    'column_h': 'expected_column_h_value',
    'column_e': 'expected_column_e_value',
}

Found similar results: [
    {
        'column_a': 'column_a_value',
        'column_h': 'column_h_value',
        'column_e': 'column_e_value',
    }
]

@taylorotwell taylorotwell merged commit 7dc8cc0 into laravel:9.x Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants