Skip to content
Advertisement

Feature test for HTTP RESTful API on Laravel 8

I’m trying to write a feature test in RESTful API that I have been created in laravel 8. I have ChatController that gets user chats by getUserChats method:

JavaScript

that called by this route:

JavaScript

this is my feature test on this route:

JavaScript

the test runs successfully by this green output OK (1 test, 3 assertions) but I don’t know this is the best way to implementing that.

so the question is that anything else I have to do in my test? am I doing the right feature test?

Advertisement

Answer

Generally, you want the scope of your test to be as small as possible. The test case that you provided is currently testing two separate api endpoints:

  • Creating chats
  • Getting chats

The issue with this is that if code related to creating chats breaks, your test case related to getting chats will fail.

I would recommend splitting these into two separate test cases. This will allow you to trust that if your test case for getting chats fails, it’s as a result of the code relating to getting chats failing rather than code unrelated to this endpoint.

I would also recommend using setUp and tearDown methods to ensure that your test case leaves the database in the same state that it found it. This ensures that other test cases will not have to concern themselves with the data from this test. This means the setUp method should do things such as:

  • Creating users
  • Creating chats

The tearDown method would be responsible for deleting the users and chats.

Additionally, you should test the format and data that is output by your endpoint. At the moment you are only checking for a 200 response code. If your api were updated to return a list of users rather than chats, the test case related to getting chats would still pass as it does not actually validate the data that is being returned.

EX:

JavaScript

One thing to note about the example code is that it does not use the rest api to create the user or the chat but instead uses pseudocode controller methods to ensure that it isn’t dependent on the functionality of the chat api for testing.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement