Support the ongoing development of Laravel.io →
Database Testing
Last updated 2 years ago.
0
  1. In a test, you have to ensure that your code is doing what you want. Of course you're not going to tests methods like "save" (there are already tests for that).

  2. You use Mocks.

Greetings.

Last updated 2 years ago.
0

Thanks for reply!

  1. What common tasks developers tests in an interaction with databases in this case?
  2. I think that only "mocks" don't answer my question. ;) If someone has a more complete answer I will be thankful.

Thanks,

Marcel

Last updated 2 years ago.
0

Ok, so for example, let's say that you have a method in a model that does the following:

  1. Takes an User instance
  2. If it exists, change the password to 'asd'
  3. If it doesn't, create a new user and return it

So what you test is:

  1. Call the method with an existing user and then check it's password, is it 'asd'?
  2. Call the method with a non-existing User and check the return, is it a new user?

Now, a query is involved here (set password = 'asd'), we could use real db, but when tests grouw up, it will take too much time.

What we do instead, we use mocks. With mocks, you ensure that a method is being called but the actions are not performed.

This is a very nice tutorial about testing in laravel: http://code.tutsplus.com/tutorials/testing-like-a-boss-in-laravel-models--net-30087

Hope it helps.

Greetings.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.