PHPUnit in Laravel: Simple Example of Why/How to Test

PHPUnit in Laravel: Simple Example of Why/How to Test

Laravel Daily

4 года назад

65,471 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@necrotikS
@necrotikS - 30.08.2022 13:39

I'm having a very hard time trying to understanding the difference between Unit tests and Feature/Integration tests, mainly in Laravel... Why is this a Unit tests and what would be an Integration test?

Ответить
@raminjavi1708
@raminjavi1708 - 03.07.2022 08:25

I like the way you teach

Ответить
@augischadiegils.5109
@augischadiegils.5109 - 29.04.2022 01:23

❤️❤️❤️❤️

Ответить
@kickass1179
@kickass1179 - 04.04.2022 11:42

Great video, but it is painfully TIME CONSUMING to write that much code (unit test code) for a simple if else statement. What if we needed to do this for our WHOLE source code? I understand the advantages but is it worth the disadvantage? Maybe do this selectively?

Ответить
@mrsakurai4411
@mrsakurai4411 - 02.03.2022 11:33

Wow, this is so informative and elaborate. Thanks!

Ответить
@vinhvo4816
@vinhvo4816 - 12.12.2021 02:19

Why are you opening folder ?

Ответить
@adarshchacko6137
@adarshchacko6137 - 20.11.2021 18:16

What is your suggestion for a scenario where I have to test a set of APis which have to pass through various middlewares (viz. jwt, roles and so on) and recaptcha... These APIs are supposed to call a service which eventually calls another set of external APIs (viz. google APIs) and the values returned by the external APIs are stored in the DB...

How do I go about testing that? Please advise.

Would really appreciate if you could cover this.

Ответить
@nikza1936
@nikza1936 - 21.10.2021 18:35

What about using .env.testing ?

Ответить
@Atiradeonvideo
@Atiradeonvideo - 23.09.2021 17:30

This is why you should always write your test before coding. Great work!

Ответить
@ShinjaeKang
@ShinjaeKang - 19.09.2021 12:21

Thank you very much for great tutorial video!

Ответить
@ghazyy1
@ghazyy1 - 17.09.2021 22:24

thank you sir.. this is really good and helpful tutorial

Ответить
@SreekalaA
@SreekalaA - 19.08.2021 07:52

Thankyou for making it simple and clear!

Ответить
@phil86_
@phil86_ - 29.07.2021 22:08

Very nice tutorial

Ответить
@amirhossein-ni3ip
@amirhossein-ni3ip - 12.07.2021 17:19

is it ok to touch the database in unit tests?

Ответить
@asepookurniawan4453
@asepookurniawan4453 - 07.07.2021 11:04

it so simple and easy to be understood. nice video.

Ответить
@HoratioKJV
@HoratioKJV - 03.06.2021 19:12

Writing unit test code seems more complicated and time consuming than writing the app code itself... And how can you be sure your unit test code is valid, are you going to write test code for your test code and so on? You surely can't rely on phpunit, and you still have to test your raw code anyway. The only true way to know if your code works is to run it, in my opinion.

Ответить
@samiullah0011
@samiullah0011 - 01.06.2021 23:29

what are benefit of using unit tests?

Ответить
@samiullah0011
@samiullah0011 - 01.06.2021 23:27

sir when we have to use unit tests?

Ответить
@nathanbarlowjr.3127
@nathanbarlowjr.3127 - 21.05.2021 01:25

Help!
I cannot access my imported classes within the tests.
I get: Error: Call to a member function connection() on null

Ответить
@giuseppeloarte9133
@giuseppeloarte9133 - 18.05.2021 05:49

Awesome! Very well explained, thanks!

Ответить
@olegpristashkin9078
@olegpristashkin9078 - 14.05.2021 18:48

What is difference between unit and feature tests?

Ответить
@aspirinemaga
@aspirinemaga - 10.05.2021 20:06

why everyone is using dark theme ?? I can barely see the text during the daylight

Ответить
@SergeyEmelyanov86
@SergeyEmelyanov86 - 05.05.2021 21:53

What do you think about PEST package and it's syntax? Some developers think that in future laravel versions it will be default test package

Ответить
@alila3883
@alila3883 - 28.04.2021 05:32

Nice example 👍

Ответить
@linjustek
@linjustek - 24.04.2021 19:49

It is an integration test, not the unit. In the unit test, you should not use a database or such. You should be able to mock the data source. If you'd do it using TDD, you'd write an empty class and then tests where you would call that class and not yet existing methods, inject class with existing dependencies that would be mocked for sourcing data and create data sets that would be generated or with faked data as you said. :-)

Unit tests should not test the database which is the case in this example...

But guess its preferences. Without the database layer, unit tests would run much faster because they wouldn't need to bootstrap the whole app to test a single function or class.

If you're using facades in functions you can be assured that this function cannot be unit tested, because the whole app container handler will have to be bootstrapped.

Basically, you cannot unit test as well controller actions. So all business logic should happen before controller action. So service should be injected to let's say repository and then service should be mocked to return dummy data for testing on required method calls.

Ответить
@takieddine1341
@takieddine1341 - 22.04.2021 12:39

Great Work so simple and easy to be understood 🙌

Ответить
@rodrigo176
@rodrigo176 - 09.04.2021 11:15

I learned so much from your channel! Thank you! Here's hoping I ace the Laravel coding challenge for a job im applying for.

Ответить
@edpogi7934
@edpogi7934 - 07.04.2021 02:50

what css framework did you use for this lesson?

Ответить
@zHqqrdz
@zHqqrdz - 06.04.2021 16:50

It's a nice video, but this is not unit testing. It's a feature test. It won't work without a working database, and we're not only testing the service logic, but we're also testing the Income table, and the query to find the sum amount. We could pass in the user and construct the object with the Income class injected to mock it. We could also simply delegate fetching of the Income total amount to the Controller, while using query scopes (Income::forUser($user)->fromLastYear()) since this is the application level as opposed to this service which is very much in the Domain.
Not saying this is bad or anything (it's amazing for beginners), but since I was looking for real-world examples of unit testing in Laravel, I wanted to add that comment. Cheers.

Ответить
@zeeshanhaider4602
@zeeshanhaider4602 - 29.03.2021 13:29

So in simple words, unit testing is important when you want to trace minor error which can not only occur due to the bad logic but also invalid data.

Ответить
@mohammadmohammadi2724
@mohammadmohammadi2724 - 24.03.2021 16:24

Hello I'm Iranian We Can't Buy your courses(Because of sanctions) if you can have bitcoin pay in your site it will be excellent.

Ответить
@raimonds.L
@raimonds.L - 23.03.2021 11:00

I remember doing unit tests on sqlite and dev running on mysql

Ответить
@developerstutorial8821
@developerstutorial8821 - 15.03.2021 08:42

cool

Ответить
@harshittamrakar4456
@harshittamrakar4456 - 14.03.2021 17:10

How are tests are performing so fast ?
On my local it takes lots of time to load phpunit and then runs test
Please help

Ответить
@DevPasjaByDanielSmigiela
@DevPasjaByDanielSmigiela - 09.03.2021 11:44

in laravel 8 and phpunit 9.5.2 syntax factory(Model) not working. I must using Model::factory() - and its working fine.

Ответить
@5335resan
@5335resan - 11.02.2021 13:18

It was one of the best I have seen till now. Thanx man. Good job

Ответить
@AtomCoder
@AtomCoder - 12.01.2021 05:57

Superb content. Thank you!

Ответить
@zainzulifqar
@zainzulifqar - 27.12.2020 15:36

Now i know why unit testing is important

Ответить
@mojtabavahdati7648
@mojtabavahdati7648 - 16.10.2020 15:14

Perfect video. Thanks.

Ответить
@franeklubi
@franeklubi - 15.07.2020 19:00

Wow! Thank You! That was very informative

Ответить
@dgloria
@dgloria - 11.06.2020 22:08

Hi, I have a lot of code in my controller/index, which builds me a json api from all kinds of joined tables. Is there a way to assert if all the json data is provided, that is required by the frontend, and return some error if any of these json items aren't accessible? It would be easier to test like that rather than always swap between the frontend and the backend for errors. Thank you.

Ответить
@JohnnyBigodes
@JohnnyBigodes - 14.05.2020 20:39

This was a good example... Loved it... Could you share the difference between Unit and Feature tests?? There is a lot of wrong asumptions out there.

Ответить
@manuelregidor6025
@manuelregidor6025 - 12.05.2020 14:28

Hi Povilis. Do you know any "trick" to clone a database as you mention? I have a production database and I want to start testing with a clean database as you mention in the video! (I have no migrations as is a legacy project)

Ответить
@ajithlal1688
@ajithlal1688 - 11.05.2020 08:46

Thanks for the video. Waiting for more videos in this and services.

Ответить
@vovkko
@vovkko - 10.05.2020 11:10

why not artisan test

Ответить
@tkouleris
@tkouleris - 10.05.2020 09:16

I usually go with the repository pattern, injecting a repository interface into the service and at the Unit test I use a mock of that to get data from the "database". According to Uncle Bob a Unit Test should be able to work either you have a database or not having one. Also by randomly generating data you can not be sure in every test what the outcome will be. Maybe works in this case but if you wanted to check the total amount of a period it would not work unless you insert specific data to test. Anyway, I really like your videos, there are very helpful.

Ответить
@kelvezu1585
@kelvezu1585 - 10.05.2020 08:53

This is what I'm looking for, hoping to see more of these. Thanks, Laravel Daily for sharing your daily knowledge. Godbless.

Ответить