0

I am making a project using the Laravel framework.

I plan to create unit tests for my project. But I feel I don't really have an idea what to test because I mostly use library or framework functions. Even though I create my own functions, they usually are compose of functions called from Laravel and libraries I use. Here are some examples.

Adding a user

My application takes inputs from an ajax request, validates the fields and add them to the database.

  • The retrieval of fields from post data is handle by laravel (Request::all)
  • The field validations are handled by laravel's validator object.
  • The save method from an Eloquent object saves to the database.

What could I test here since all the objects and functions I used (validator, eloquent) are supposedly tested already by the framework authors?

Payroll computation

User submits an excel file that contains values. I would then make computations on each row and save each to the database.

  • Excel file handling and error handling are already handled by the phpspreadsheet library.
  • Any validations of dates in the file can be handled by the Carbon library.
  • Computations are handled by me. Know other library functions are used except the php built-in functions.
  • Validations are handled by Laravel's validator object.
  • Batch inserts are handled by Laravel's query builder.

The only thing here I think could be tested are my user-defined computation functions because those are created by me.

Are my observations correct?

2

NO.

By the same reasoning, you could argue that no unit tests are ever needed since the whole PHP code was written by very experienced and talented people, which probably tested everything themselves.

Adding a user (7 tests)

My application takes inputs from an ajax request, validates the fields and add them to the database.

Tests you should do :

  • What should happen when a validation rule is broken ? What kind of HTTP status do you expect ? What response should be returned by the controller ? (2 tests)
  • Ensure that every validation rule has been implemented to the validating code. Can you put an email that's not a valid email ? Can you have a password shorter than 4 chars ? (X tests, let's say 2 even though there'll surely be more)
  • Does the database contain the right data after the request is successfully completed ? What response do you expect from the controller after success ? What status code ? (3 tests)

Payroll computation (16 tests)

User submits an excel file that contains values. I would then make computations on each row and save each to the database.

This one is a bit more complex since you didn't provide many details. But it's easy to assume what you should test :

  • Validation about the input, same as above, what response to expect, what status code in case of failure ? (2 tests)
  • What kind of validation rule should be implemented ? Should the excel file be under or over a certain size ? Should it have a certain type ? (X tests, let's say 2 even though there'll surely be more)
  • Which computations should be used ? (1 test)
  • For all these computations, prove they work. (X tests, let's say 10)
  • Is the data correctly inserted in the database ? Are there cases when it should not be inserted ? (1 test)
  • What response do you expect from the controller after a success ? (1 test)

As you can see, there are a lot more tests than zero, and I don't even know what kind of project you're working on or any specifics about it. So you could definitely multiply that number by 2 or 3.

When you test your application, you don't test that the libraries you're using work. You test that your application works. That's a whole different story.

  • Oh my god! Those are so many tests! Is this really needed? Wouldn't doing these consume gigantic time? – morbidCode Oct 25 '18 at 13:43
  • 1
    Well not that much actually, I think it'd take 15-20 minutes to write the tests of adding a user for example. The most time consuming is to gather the exact requirements to fulfill proper tests. And the time invested in writing those tests is time that's never lost again manually testing these things when something breaks. 100% worth it ! – Steve Chamaillard Oct 25 '18 at 14:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.