Basic CRUD With TDD In Laravel 5.8 Tutorial

TDD crud tutorial: Unit testing is testing parts of the code. It is fundamentally testing individual units or functions of the code that makes up the entire system. If you haven’t tried TDD Unit Test before, then you have come at the right place.

Here, I am going to show you Basic TDD crud tutorial in Laravel. You will learn How TDD(Test Driven Drive) unit test performed after completion of this tutorial.

What is TDD? Why TDD?

TDD is Test Driven Drive unit test. It tests your classes, Models, Repositories, etc.

TDD unit tests

are always written before the source code. It will initially fail So, you will get better idea about which scenarios should be covered and fixed in actual source code.

It is essential to check some condition and make the actual code more robust and bug-free by applying unit tests in your system.

TDD unit test steps:

  • First write test case for individual module or functions base on different conditions and functionality.
  • Then run the test cases if fails then fix failed test case.
  • Now, Write source code to make the test case pass.

Basic TDD crud tutorial with Example

#1: Create a new laravel project

To create a new laravel project type the following command.

installing tdd crud project

#2: Change Database Configuration

Now, Next step is to configure database which is an essential step because user’s data is most important for any web application and it should be stored into database So, You have to create a database first using PHPMyAdmin or Sequel Pro or any database tool.

After creating the database. You need to change some configuration in your .env file to use the database. You can find the .env file located in your project root folder.

Here, You need to change the database name which you have given during database creation and also change username and password according to your MySql configuration. after successful configuration, you will be able to connect with your database.

#3: Create table and related model and controller

Here, We are creating

Laravel 5.8 CRUD

application for article. So, We are going to create the model with the schema for articles table through PHP artisan command.

Now, using the above command new migration for the article model will be generated under the database folder of your project. You can add fields according to your requirement in the migration like this.

Then, run the following command again

After migration adds below code to Article.php model to prevent some mass assignment exception during creating a record.

#4: Generate Model Factory

We are now ready to generate a model factory for our database table. Model Factory will be used to seed our table with test data. We will also make use of Model Factory in our TDD tests.

Let’s now generate Model Factory for our Articles table. Run the following command on your terminal at the project root directory.

Above, the command will create a new factory model under database > factories folder

We don’t want to work with our local database since that might be already populated with other data.

So, For the tests, I will use MySQL tdd_demo database. Let’s make some configuration changes to the phpunit.xml file, which is located at the root folder of your laravel application. and set the environment variable as given below.

#5: Read All Articles

Let’s create our First Test. Go to your tests > Feature directory and create a new file with name TasksTest.php

In the above code, DatabaseMigrations will refresh the database every time you run the test cases.

Let’s start by adding our first feature test in the Test Class. The first feature we can think of is that the user should be able to read all the tasks in the database. Add a new test that says exactly the same.

Running the test Now to run this test, go to your application terminal and run the following command.

#6: Create Article Test Case

Now, let’s make a test case for creating an article in TasksTest.php file where we assert that a user can create new task in the database.

To run the above create test case, Run the below command in terminal.

Run the test, and it may give you red color because we check one article in the assertEquals method in the above code but we did not create a new task yet.
tdd create test

So, First, create a new article then run the test again so, it will turn a green color.
create test case

#7: Update Article Test Case

Let’s go to the update part of our CRUD application. A user should be able to update the article. Write the below test case for updating an article in the TasksTest.php

To run the above update test case, Run the below command in terminal.

update test case

#8: Delete Article Test Case

Let’s go to the delete part of our CRUD application. A user should be able to delete an article. Write the below test case for deleting an article in the TasksTest.php

To run the above delete test case, Run the below command in terminal.

delete test case

In the tutorial, we have completed CRUD Test Driven Drive. But you can write various test scenarios for a single method of the controller such as validation scenario and all other errors you can think of.

Finally, We have completed

TDD crud Tutorial with Example

in pretty and straightforward steps. I hope you learned from this article. Thank you.

About the author

Siddharth Ghedia

Hi, my name is Siddharth. I'm from Rajkot, Gujarat, India. I'm passionate about Laravel. I have over 4 years of experience in Web Development. I work in the technologies like: Laravel, Vue, React, React Native, Angular, IONIC.

I've created this blog for sharing my knowledge with other developer who are willing to start and build their career in the technologies that I work in.

Leave a Comment