Laravel-ide-helper: Use snapshot assertions for tests

Created on 19 Jun 2020  路  3Comments  路  Source: barryvdh/laravel-ide-helper

Hey,

while creating #955 I was blown by this crazy long HEREDOC variable. For me these tests are perfect usecases for snapshot assertions which would make everything more readable.

https://github.com/spatie/phpunit-snapshot-assertions

Is there an interest to change this?

Most helpful comment

Hello there 馃憢

I was the one who created the tests in its current form; I was not aware of the snapshot package and put more emphasis on getting _some_ tests into this library (there were none regarding this before).

That said: I think it makes sense :) The HEREDOCs are truly annoying to "maintain".

Thanks!

All 3 comments

What would that look like?

The expected string would live in its own dedicated file and in test you call one of the assertSnapshot() methods.
The file is auto-generated on first run and can be auto-updated by adding options to the phpunit command.
For me the main benefit is that the expected isn't an enormous multiline heredoc in the test but a dedicated file. This makes writing the real test much easier because it's not this cluttered.
It also makes the diffing easier because test and expected are two different files.

For me snapshots are the way to go for assertEquals() on larger strings. There are some special drivers for HTML, yaml and do on - but it's also possible with plain text comparison.

This will be the line I think should be sufficient in the test.

$this->assertMatchesTextSnapshot($actual);

Hello there 馃憢

I was the one who created the tests in its current form; I was not aware of the snapshot package and put more emphasis on getting _some_ tests into this library (there were none regarding this before).

That said: I think it makes sense :) The HEREDOCs are truly annoying to "maintain".

Thanks!

Was this page helpful?
0 / 5 - 0 ratings