Skip to content

[7.x] Add assertDatabaseCount assertion#32597

Merged
taylorotwell merged 3 commits intolaravel:7.xfrom
christophrumpel:feature/addDatabaseCountAssertion
Apr 29, 2020
Merged

[7.x] Add assertDatabaseCount assertion#32597
taylorotwell merged 3 commits intolaravel:7.xfrom
christophrumpel:feature/addDatabaseCountAssertion

Conversation

@christophrumpel
Copy link
Contributor

This PR adds a new database assertion called assertDatabaseCount($count).

Next to the given assertDatabaseHas or assertDatabaseMissing assertions, it is often useful to just check the number of entries in a database table. This is possible in different workarounds already, but it would be more convenient to provide a simple helper through Laravel as well.

Here is an example of how to use it inside a test:

MyCustomUserFactory::new()
    ->times(50)
    ->create();

$this->assertDatabaseCount('users', 50);

And this is how a failure message would look like:

Failed asserting that table [products] matches expected entries count of 3. Entries found: 1.

@christophrumpel christophrumpel changed the title Add assertDatabaseCount assertion [7.x] Add assertDatabaseCount assertion Apr 29, 2020
@taylorotwell taylorotwell merged commit d922f2d into laravel:7.x Apr 29, 2020
@taylorotwell
Copy link
Member

Thanks

@christophrumpel
Copy link
Contributor Author

👍Do you want it to be added to the docs as well? https://laravel.com/docs/master/database-testing

@veneliniliev
Copy link
Contributor

this is good feature but can you add array $data for additional wheres for count like other assertDatabase* methods?

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.

3 participants