TDD how do you test backend and frontend Hi, I'm new to TDD. As I'm developing my app at the moment I'm only using phpunit with assertions and HTTP tests.
At this moment I'm testing features, functionalities and some information that is rendering on blade templates with HTTP tests.
Do you use this tests for what's rendering on the browser, like HTTP tests or do you write separate tests in another package like Laravel Dusk?
I wanted to find some guide on what to test and how to test it. For example, use phpunit to test features and use dusk to test frontend and browser...
When it comes to the frontend I only test that certain things are present or not present.
Something like this
/**
* @test
*/
public function user_can_create_authors()
{
$this->signIn();
$response = $this->get('/authors/create');
$response->assertSee('name="first_name"', false);
$response->assertSee('name="last_name"', false);
$response->assertSee('name="_token"', false);
$response->assertSee('input type="submit" value="Save"', false);
}
and something like this
/**
* @test
*/
public function after_creating_an_author_the_user_is_redirected_to_the_authors_index_view_and_success_message_is_shown()
{
$this->signIn();
$author = factory(Author::class)->make();
$response = $this->post('/authors', $author->toArray());
$response->assertStatus(302);
$response->assertLocation('/authors');
$response = $this->get('/authors');
$response->assertSee($author->name . ' successfully added.');
}
To test the functionality I suggest using Cypress
https://www.cypress.io/
Great information, and do you create separate tests for user roles?
For example if you have 3 different users with different permissions using policies, do you write 3 tests for the same feature?
Thank you.
@charrua I’m not sure what difference “backend” and “frontend” makes; tests just perform actions and then assertions. Doesn’t matter if you’re testing a “front” end route or a “back” end route.
Yes I would most likely write tests like
an_admin_can_update_blog_post
an_editor_can_update_blog_post
a_user_cant_update_a_blog_post
If all user type can update the post I would write one test that asserts that they can
any_user_can_update_a_blog_post
@martinbean you are right, my differentiation on using the words “backend” and “frontend” has nothing to do here, it was a mental map I have from the app. I was thinking on user roles when referring to those.
Thank you @martinbean and @tray2 for the answers and help on this issue!
@charrua Yeah, feel free to organise your tests like that, but the approach doesn’t really change between testing the “front” and “back” of your application :)
Just write feature tests that make requests and either test for a success or error response:
$this
->actingAs($admin);
->post('/admin/products', $validData)
->assertSessionHasNoErrors()
->assertRedirect();
$this
->actingAs($admin);
->post('/admin/products', $invalidData)
->assertSessionHasErrors(['fields', 'that', 'should', 'fail', 'validation']);
Please sign in or create an account to participate in this conversation.