Laravel 5.8 Tutorial From Scratch - e49 - Testing 101 Using PHPUnit

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024

Комментарии • 83

  • @linguapharm
    @linguapharm 5 лет назад +6

    I MISS YOUR VIDEOS SO MUCH!
    I suggest a video with relationships: product, category, subcategory, ( to help for laravel eCommerce )

  • @akashthoriya
    @akashthoriya 4 года назад +9

    in laravel 7,
    run "php artisan test" instead of "vendor/bin/phpunit"

  • @RoisulAsolole
    @RoisulAsolole 5 лет назад +2

    Great tutorial, I suggest you to make such a integration with third-party like pusher, payment gateway and etc. Thanks a lot.

  • @aseemlalfakawma5084
    @aseemlalfakawma5084 4 года назад +1

    This is the part of testing that I do not get. What should we test and what should we not test. I mean we can't test everything right? Especially if we work an hourly freelance project, writing to test every little thing is going to cost our clients too much. Or are you showing these things as an example, like asserting that the name is 3 letters, then asserting if the name is passed, I mean you can't do this with each field right. That would be too much. But where do we draw the line?

  • @chichakjamshidi8802
    @chichakjamshidi8802 4 года назад

    First of all I want to appreciate because of your high quality and useful tutorials ... In this episode u wrote some tests that triggered a question that why we should test this obvious actions ? We easily run app on browser and see the results ...are these tests essential for development ?

    • @RaduRadonys
      @RaduRadonys 4 года назад

      You should write test for all actions, no matter how obvious they are. Then you can go to sleep without thinking that an error has slipped away undetected because of human error/logic/thinking process. It's a tedious work, everyone agrees, but if you are serious, you should do it.

  • @mavbcn1790
    @mavbcn1790 5 лет назад

    Great! as Always!
    It would be great to have some videos about creating a complete API with Laravel.
    thanks so much for your help

  • @kiprasbiel
    @kiprasbiel 4 года назад +1

    For those who actually use MySQL as their DB and get error when trying to run tests in sqlite, I suggest to use MySQL for testing also.
    You need to create separate database (smth like mydatabase-test);
    In phpunit.xml bellow (inside tags) add:
    So what this does is it uses MySQL instead of sqlite. I think this is slower, but it eliminates any compatibility issues.

    • @kendallhayes4917
      @kendallhayes4917 3 года назад

      Thanks, I will try this. I was still getting the error user table not found. Do you know if we should add and

    • @kiprasbiel
      @kiprasbiel 3 года назад

      @@kendallhayes4917 no, you don't need to add :memory: , because MySQL doesn't work in memory as SQLite.

  • @TDarkCoder
    @TDarkCoder 5 лет назад +1

    I have a telescope switched on in my application, but phpunit is still working properly! Don't you know the reason Mr.Gonzalez?

  • @adam192021
    @adam192021 5 лет назад +3

    The BEST as always!

  • @Leo-jh3nj
    @Leo-jh3nj 4 года назад +1

    when I run phpunit test in console, Vendor/bin/phpunit --filter only_logged_..._list, it shows : no test executs. I finially find this problem solved method.
    Add test prefix to any test function in Test.php. like this : test_only_logged_..._list. It's gain the same effects as videos shows. Maybe the version of Phpunit : my version is 7.20. CHeers , hopefully helpful.

  • @uros.u.novakovic
    @uros.u.novakovic 5 лет назад

    With the current version of Laravel and Telescope I see no issue with running PHPUnit. Looks like the new version of Telescope is no longer causing issues.

  • @busuyiowoyemi5368
    @busuyiowoyemi5368 5 лет назад +3

    Please, give us an accounting system or ecommerce app with Laravel. Your teaching skill has no equal.

  • @geraldterencio8635
    @geraldterencio8635 2 года назад

    when I tried to execute "vendor/bin/phpunit" it gives me "vendor is not recognized as an internal or external command,operable program or batch file."

  • @salmankabirshouvo
    @salmankabirshouvo 3 года назад

    There is an error in that store array you have put 'active' 2 times but php unit didn't catch that mistake!
    Why ?

  • @robbradley1337
    @robbradley1337 5 лет назад

    Dang it. Your too late for me. I've been binge watching PHPUnit videos at Laracasts. Thanks for making these videos nonetheless! I'm a fan of the channel. :D

  • @MuhammadAdnan-gx6rd
    @MuhammadAdnan-gx6rd 5 лет назад +1

    Sir please make a blog for beginners #Laravel ?

  • @kingalpaca2361
    @kingalpaca2361 5 лет назад

    Your videos are awesome!!!

  • @MuhammadKhalilZarpio
    @MuhammadKhalilZarpio 4 года назад

    The latest commit is (e44 - Policies) on github, can you please commit your other changes. Thanks.

  • @BitValentine
    @BitValentine 4 года назад

    Tried adding a new test with a simple 'if string' and works. However, when I add 2nd function, it gives me an error
    PHP Fatal error: Cannot declare class App\Console\Commands
    elatationshipDaemon, because the name is already in use in ...
    I'm not calling this command at all, so its confusing me a lot.
    Code:

  • @RHCIPHER
    @RHCIPHER 4 года назад

    InvalidArgumentException: Unable to locate factory for [App\User].

  • @zahirzohair1372
    @zahirzohair1372 4 года назад

    I test API using phpunit test but i keep getting " Expected status code Ok but received 302." what is this 302 about ?

  • @atiquechowdhury8910
    @atiquechowdhury8910 4 года назад +5

    Before Starting this lesson, I wish to let everyone know that laravel's recent version has changed the naming standard for test cases. SO if you use:
    public function only_logged_in_user_can_see_customers_list()
    {
    // code here
    }
    IT WILL FAIL. It will not be detected by PHPUnit at all. The new format is that you use prefix the function name with "test" so it should be named:
    public function testonly_logged_in_user_can_see_customers_list()
    {
    // code here
    }

    • @dgloria
      @dgloria 4 года назад +2

      /**
      * @test
      */

  • @Innoox
    @Innoox 5 лет назад +3

    Again, for the windows users:cls && "./vendor/bin/phpunit" --options [NAME_OF_OPTION]
    although:
    file: phpunit.xml

    // :memory: - for me doesn't work
    file: /tests/Features/CustomersTest.php
    $this->actingAs(factory(\App\User::class)->create());
    // Have to add \App\Users or use App\User;

  • @davidvanicek3123
    @davidvanicek3123 4 года назад

    Great video

  • @linasgutauskas5528
    @linasgutauskas5528 4 года назад

    Hello. Thank you for tutorials. When testing, that user can add new customers. Wouldn't be better instead of "assertCount" to use "AssertDatabaseHas" and compare if input matches with database data?

    • @bradleylatreille5480
      @bradleylatreille5480 4 года назад +2

      assertDatabaseHas seems to be more geared towards ensuring the database contains specific values while count allows you to check for a specific count. assertDatabaseHas requires a table name as well as the associative data array so it would be easier to use a count in his case.

  • @HussamAdil
    @HussamAdil 5 лет назад

    how to deploy laravel on sheared host ? Really waiting for you videos

  • @Anonymous-jd3zc
    @Anonymous-jd3zc 5 лет назад +1

    Thank you my good sir

  • @gravityarm9240
    @gravityarm9240 3 года назад

    This video actual for laravel 8 ?

  • @lordgodgiven
    @lordgodgiven 5 лет назад

    Hello,
    First of all thanks for your scracth tutotrial, i lean many things. I've a question, where to put logic business of my laravel application? In controller? In model? or Using design pattern repository.
    Thanks

    • @CodersTape
      @CodersTape  5 лет назад +1

      That’s a great question but a difficult one to answer because there’s no single correct answer for this. The true answer is, it depends. One approach that has worked for me is, creating a directory in my app directory for project classes. This will include classes for connecting to APIs or logic specific to the project. You can then bind them into the service container and have laravel serve them up for you in the controller using dependency injection.
      Again, this is my approach, it doesn’t mean there aren’t 10 different ways to do it.

    • @lordgodgiven
      @lordgodgiven 5 лет назад

      @@CodersTapeThanks for your rapid reply, your approche looks like java approche with service layer, anyway as you say there are many, so one more thanks. Hope that you going deep in test driven developpement because it's no easy to write test before code. Great job!!!!

    • @CodersTape
      @CodersTape  5 лет назад +1

      You’re right. It’s very difficult to imagine your code before writing it but once it clicks, it’s eye opening how much better your code gets.

  • @ivanc.6064
    @ivanc.6064 4 года назад

    I'm getting the following error:
    Error: Call to undefined method Tests\Unit\CustomersTest::get()

  • @creepzy7280
    @creepzy7280 5 лет назад

    Maybe i comment to much late but i've Database [:memory:] not configured. error

  • @lubomirhorvath823
    @lubomirhorvath823 5 лет назад

    Really Thank you for this tutorial. It´s one of the best I have ever watched.
    I got a Failure in second case testing :
    There was 1 failure:
    1) Tests\Feature\CustomersTest::authenticated_users_can_see_the_customers_list
    Response status code [404] does not match expected 200 status code.
    Failed asserting that false is true.
    Any advice? Thanx

    • @dgloria
      @dgloria 4 года назад

      404 is page doesn't exist. Check the logs.

  • @jfordgaming9615
    @jfordgaming9615 4 года назад

    Hi I got error when I run vendor/bin/phpunit, error:"Tests\Feature\ExampleTest::testBasicTest
    Expected status code 200 but received 302.
    Failed asserting that false is true."

    • @stewheart
      @stewheart 4 года назад

      If your code is similar throughout all the tutorials, I think the basic idea is that visiting the customers page whilst not logged in should redirect you to the login page. I had the same error as you but set the method to $response = $this->get('/customers/show')->assertRedirect('/login'); The problem is that visiting '/customers' doesn't redirect you when logged in, '/customers/show' does. I realise your post was 6 months ago but I'm so happy I've identified and fixed something... doesn't happen often!

  • @delvinduel189
    @delvinduel189 4 года назад

    Tests\Feature\CustomersTest::a_customer_can_be_added_through_the_form
    Intervention\Image\Exception\NotReadableException: Image source not readable
    I get this error ..after setting up authorizationException..If anyone know the answer please share..

  • @pawelkubrak547
    @pawelkubrak547 4 года назад

    Can you make more videos, please !!!

  • @thanasakSrisuwan
    @thanasakSrisuwan 5 лет назад +1

    i want laravel shoping cart tutorial so bad. 😢

  • @MagnusPicard
    @MagnusPicard 5 лет назад

    Hi Victor,
    I am messing around with the test - I tried to follow your guide but I am struggling.
    I added

    to my phpunit.xml file and my testfile looks like this name is UserTest.php
    /** @test */
    public function only_logged_in_users_can_see_the_dashboard()
    {
    $response = $this->get('/home')->assertRedirect('/login');
    }
    /** @test */
    public function authenticated_users_can_see_the_dashboard()
    {
    $user = factory(User::class)->create();
    $this->actingAs($user);
    $response = $this->get('/home')->assertOk();
    }
    If I run phpunit it gives me
    1) Tests\Feature\UserTest::authenticated_users_can_see_the_dashboard
    Response status code [500] does not match expected 200 status code.
    Failed asserting that false is true.
    Do you have any suggestions for me?
    Thanks, Timo

    • @CodersTape
      @CodersTape  5 лет назад +1

      Yes. Inside of your test add the following line
      $this->withoutExceptionHandling();
      Then run your test again, that will give you a real error.

    • @MagnusPicard
      @MagnusPicard 5 лет назад

      @@CodersTape thanks for the quick reply I will test this tomorrow just left work :)

    • @MagnusPicard
      @MagnusPicard 5 лет назад

      @@CodersTape I just tried your suggestion
      1) Tests\Feature\UserTest::authenticated_users_can_see_the_dashboard
      ErrorException: Undefined index: REMOTE_ADDR (View: /home/vagrant/code/Nathan/resources/views/layouts/app.blade.php) (View: /home/vagrant/code/Nathan/resources/views/layouts/app.blade.php)
      Caused by
      ErrorException: Undefined index: REMOTE_ADDR (View: /home/vagrant/code/Nathan/resources/views/layouts/app.blade.php)
      Caused by
      ErrorException: Undefined index: REMOTE_ADDR
      in my layouts/app.blade.php I have created a user menu which also includes
      {!! $_SERVER['REMOTE_ADDR']; !!} the show the user his IP (its a intranet project) This call creates the error. After removing this - All tests running as expected.
      Thanks for the advice with $this->withoutExceptionHandling(); this helped a lot finding the problem.
      Timo

  • @jamieb3451
    @jamieb3451 5 лет назад

    I'm getting a no tests found when I try to run the first test. Do you have any idea why this may be the case ?

    • @CodersTape
      @CodersTape  5 лет назад

      Did you rename the ExampleTest.php file? If so, make sure that it still ends in Test.php
      For example, Customers.php == not valid test file name
      CustomersTest.php == valid name

    • @CodersTape
      @CodersTape  5 лет назад

      And make sure that your test methods in the file have the /** @test **/ at the top

    • @jamieb3451
      @jamieb3451 5 лет назад

      @@CodersTape Yes, I have it set as PostsTest.php and it seems to still give me the same problem.

    • @CodersTape
      @CodersTape  5 лет назад

      Do you have the annotation on the function? Or does it start with test?

    • @jamieb3451
      @jamieb3451 5 лет назад

      @@CodersTape This is what I have written:
      class PostsTest extends TestCase
      {
      /**@test **/
      public function only_logged_in_users_can_see_the_posts(){
      $response = $this->get('/posts')
      ->assertRedirect('/login');
      }

  • @AmitMerchant
    @AmitMerchant 3 года назад

    You can use the "testdox" option for detailed test reports.
    www.amitmerchant.com/make-phpunit-test-reports-detailed-using-textdox/

  • @harisonroy6001
    @harisonroy6001 5 лет назад

    when will you bring for us a full project?

    • @CodersTape
      @CodersTape  5 лет назад +2

      Already working on it for the channel.
      Also, I just authored a 4.5 hour course for freeCodeCamp. It will be posting this week.

    • @MuhammadAdnan-gx6rd
      @MuhammadAdnan-gx6rd 5 лет назад

      What's included in ?

    • @CodersTape
      @CodersTape  5 лет назад +1

      @@MuhammadAdnan-gx6rd that's an instagram clone project from scratch.

    • @MuhammadAdnan-gx6rd
      @MuhammadAdnan-gx6rd 5 лет назад

      ​@@CodersTape Cool waiting,,,,,,,,,sir

  • @namachivaaya
    @namachivaaya 4 года назад

    Thank you bro

  • @digvijaychauhan2000
    @digvijaychauhan2000 4 года назад

    I am using mysql connection and when I set db_database to :memory: I run into error.
    error:
    Tests\Feature\CustomersTest::test_only_logged_in_users_can_see_customers_list
    PDOException: SQLSTATE[HY000] [1049] Unknown database ':memory:'
    Please help!

    • @CodersTape
      @CodersTape  4 года назад

      Change the connection to sqlite not mysql

  • @dgloria
    @dgloria 4 года назад

    Haha, this one overwritten my whole existing database with laravel migration data. Not happy.

  • @tothebone4613
    @tothebone4613 4 года назад

    whoa nice, is better coding while doing phpunit

  • @Aqibroker01
    @Aqibroker01 5 лет назад

    I did'nt get green after making telescope disable.???

    • @CodersTape
      @CodersTape  5 лет назад +1

      Try stopping the artisan serve command and starting it again.

  • @aizazaziz5636
    @aizazaziz5636 4 года назад

    PDOException: SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL

    • @CodersTape
      @CodersTape  4 года назад +1

      When you’re adding a column after the fact, you need to have a default value

  • @zeusgolohor8409
    @zeusgolohor8409 5 лет назад +1

    Hello sir,
    I'm getting this errors, I did exactly what you did but I still get this errors I'm working on a Windows 10 machine.
    This is my error when I run the first test only logged in users can see the customer list ()
    Error message:
    Failed to insert that two string are equal
    ---expected
    +++Actual
    -'localhost/login
    +'localhost

    • @CodersTape
      @CodersTape  5 лет назад +1

      it looks like it's not redirecting properly. Make sure that you are setting the auth middleware for authentication.

    • @zeusgolohor8409
      @zeusgolohor8409 5 лет назад +1

      Ok sir. I will give it another try and I will get back to you. Thanks for the reply

    • @zeusgolohor8409
      @zeusgolohor8409 5 лет назад +1

      It's now working. The problem was in the construction method... $this->middleware('auth')->except('index') I had to remove the except() to make it work properly. Thanks for the fast response you're the best.😀

    • @CodersTape
      @CodersTape  5 лет назад +1

      @@zeusgolohor8409 awesome! Glad to hear you are back on track.

  • @gourabkumarrout1945
    @gourabkumarrout1945 5 лет назад

    Please push this testing code to git

  • @shupesmerga4694
    @shupesmerga4694 4 года назад

    The docs never said anything about disabling telescope. I feel sorry to those who are banging their head against the docs. I'm doubting going for docs now every time I want to learn something new.

  • @youngengineer8033
    @youngengineer8033 4 года назад

    How cool u speak.