RossUK

Unit testing advice

Posted 2 months ago by RossUK

Hi, I am trying to do some unit/feature testing and take a TDD approach to a new Laravel project. I am trying to get my head around which tests I should be writing and also which tests are a feature or a unit. As an example to start off with I have a Customer model and a Contact model. When the app is finished an authenticated end user will be able to create edit and delete customers and contacts and also add contacts to a customer. Before any webforms exists what tests should/would be good to write for this.

here what I have so far but want to check I am not going completely in the wrong direction :)

<?php

namespace Tests\Unit;

use App\Customer;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;


class CustomerTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */

    use RefreshDatabase;
    use WithFaker;

    protected $customer;

    public function setUp()
    {
        parent::setUp();
        $this->customer = factory('App\Customer')->create();
    }

    /** @test */
     public function testCustomerCanBeCreated() 
     {
         $this->assertEquals(1, Customer::count());
     }

     /** @test */
    public function canEditCustomer() 
    {
        //set test data
        $data = [
            'company_name' => "New Company Inc",
        ];

        //make post request with test data to named route
        $this->post(route('customers.store'), $data)->assertStatus(201);
        $this->call('POST', '[email protected]', $data);

        //get first record
        $customer = Customer::first();

        //check database values exist
        $this->assertEquals($data['company_name'], $customer->company_name);

    }


    public function canUpdateCustomer() {}
    public function canDeleteCustomer() {}
    public function canAddContactToCustomer() {}
    public function canGetCustomerContacts() {}



}

Many Thanks

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.