PHPUnit testing FAILS ONLY when visiting the root page( visit('/') ) in Laravel 5

后端 未结 3 1847
谎友^
谎友^ 2021-01-29 00:39

I run Laravel applic ation in docker container dockervel and

the test



        
相关标签:
3条回答
  • 2021-01-29 01:20

    I think there are 2 things happening in your code snippet.

    The first thing is that you are defining the host as mysql, not the driver (which i think is the one you are trying to set). Try setting the environment variable DB_CONNECTION to mysql instead of DB_HOST.

    The second thing is that you need to add the DatabaseMigrations trait to your test class, try adding this:

    <?php
    
    // Imports
    
    class MyTest extends TestCase {
    
        use DatabaseMigrations; // This line
        ...
    
    }
    

    Without this your tests will probably fail as it will find the database, but it won't find any tables created inside it.

    Hope it helps.

    EDIT

    Best practice for testing is to either mock dependencies that need to communicate with the database, or use an in-memory database for your tests to separate them from your real database.

    In order to separate your testing database from your real database, you could use the sqlite driver in memory. Just update your sqlite driver in your database.php file like this:

    'sqlite' => [
        'driver'   => 'sqlite',
        'database' => ':memory:',
        'prefix'   => '',
    ],
    

    And use it as the DB_CONNECTION environment variable in your tests (modifying phpunit.xml):

    <env name="DB_CONNECTION" value="sqlite"/>
    
    0 讨论(0)
  • 2021-01-29 01:23

    try this: see if mysql container is up:
    docker ps
    You should see a mysql container. If not then run dstop & dup

    0 讨论(0)
  • 2021-01-29 01:29

    try an other version much easier. It is based in PHP 7 and is ready to run phpunit tests in one comand dunit

    $ dstop $ git checkout builder $ . ./aliases.sh $ dup $ dunit

    0 讨论(0)
提交回复
热议问题