1

I would like to Unit test one of my controller and I'm wondering how to do that without impacting the database. Here are my files :

Route:

Route::resource('tasks', 'TasksListController');

In my TasksListController:

public function store()
{
    if (empty(Input::get('name')) || !isset(Input::get('name'))) {
        return false;
    }
    if (Task::create(Input::all())) {
        return true;
    }
    return false;
}

My form:

        <form action="/tasks" name="task" method="post">
            <input type="text" name="name" />
            <input type="submit" name="submit" value="Submit" />
            <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
        </form>

My Unit Test:

class TasksTest extends TestCase {

    public function testAddTask()
    {
        // How can I test the task creation ?
    }
}

1 Answer 1

2

If you are testing your controller, you can mock the Task class:

Task::shouldReceive('create')->with([])->once()->andReturn('mocked-task');

Normally, a controller returns a view or a redirect object, but in this situation your return is a boolean just do the assert:

$response = $this->call('post', 'tasks');
$this->assertTrue($response->getContent());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.