1

I am using Laravel for some projects, and I am trying to create a test for a function. Here is the function in the my controller:

 public function showThoseThings()
  {
    return parent::httpRequest(
        function ($context) {
            $context->results['item'] = $this->object->findOrFail(list_id)->getElements;
        }
    );
  }

the "getElements()" function is defined in the model as follow:

public function getElements()
    {
        return $this->belongsToMany(
            'things',
            'thing_to_list',
            'list_id',
            'thing_id'
        )
                ->withPivot('position')
                ->orderBy('thing_to_list.position');
    }

Basically in the backend there are three tables a list table that is a collection of things, then there is a things table that contains multiple things associated to a list then we have the pivot table. How can I mock this with Laravel. Here is what I have been doing:

public function testShowThoseTHingsSuccess()
{
    $this->mock->shouldReceive('findOrFail')->with(1)->andReturn($this->mock);
    $this->mock->shouldReceive('getElements')->once()->andReturn($this->mock);

   $response = $this->call('GET', 'workingURI');
    var_dump($response);
     $this->assertTrue($response->isOk());

But when running phpunit in the command line I am getting:

"Unknown Error","message":"SQLSTATE[HY000] [1045] Access denied for user... Fails asserting that false is true".

1

1 Answer 1

1

I don't know what your $this->mock->.. method is doing but apparently not mocking the model you want. Because the error states that something tries to access the database and doesn't have correct credentials.

Also the lines:

$this->mock->shouldReceive('findOrFail')->with(1)->andReturn($this->mock);
$this->mock->shouldReceive('getElements')->once()->andReturn($this->mock);

Makes no sense to me, You create a mock of a model and when the methods findOrFail or getElements are triggered you return the same object.. The method getElements states to me that there should be returned some array with models..

Some more info when you want to use a testing database

When you run unit test with laravel and uses the base test class, then Laravel sets the environment to testing. Make sure that you have the right database configuration set for this environment.

You can use I.E. the following configuration to create a database in memory for testing:

// app/config/testing/database.php

<?php

return array(

    'default' => 'sqlite',

    'connections' => array(
        'sqlite' => array(
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => ''
        ),
    )
);

Don't forget to migrate your DB schema before testing, place the following line in the public function setUp() of the base test class.

Artisan::call('migrate'); 

More info about simulating the databse: NetTuts testing laravel models

Sign up to request clarification or add additional context in comments.

1 Comment

Aha, I wrote in a comment above (now deleted) that app/config/testing/database.php should be config/testing/database.php, but having done some more reading I've found that your information is correct for Laravel 4. Laravel 5 moved the config folder from app/config/ to config/, but the cascading env-specific configuration no longer works in L5, from what I can tell. However, there are third-party packages that can get this to work.

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.