Welcome Guest, Not a member yet? Register   Sign In
Testing, specifically when having database dependant code
#1

Swimming towards the deep end of the pool and now learning more about testing. 
In CI3 I used kenji's ci-phpunit-test with much success. Database testing has been made with a mysql server with a downloaded copy of the latest live database and making sure I only run non-destructive tests.


With CI4 I try to do the same, but find the following line from the $tests structure in Config\Database.php:67 a bit strange:
PHP Code:
'DBPrefix' => 'db_' // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE. 
 
I have already nuked my DB twice by setting this to blank so I'm really missing something here ;-)
Reply
#2

Couldn't you mock out your db calls?
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#3

(08-14-2019, 12:24 PM)albertleao Wrote: Couldn't you mock out your db calls?

I guess so, but wouldn't that defeat the purpose of testing, specially on Controller level?

My current approach is to use the seed function to copy the few tables I need to do testing on from the database backup (virtu12_vtester)  to the testing database (ci4test). Both databases live on the same MySQL server and ci4test user has read access to the database backup:
PHP Code:
class CITestSeeder extends Seeder
{
    public function 
run()
    {
        
$tables = [ 'country''territories' ];
        foreach (
$tables as $t)
        {
            if ( ! 
$this->db->tableExists('db_' $t))
            {
                
$this->db->query("CREATE TABLE IF NOT EXISTS ci4test.db_$t SELECT * FROM virtu12_vtester.$t");
            }
        }
    }

Reply
#4

it depends on what kind of testing you're doing. If you're doing unit tests, then you should be mocking your db, if you're doing integration tests then yea you need to use your db.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#5

(08-15-2019, 06:33 AM)albertleao Wrote: it depends on what kind of testing you're doing. If you're doing unit tests, then you should be mocking your db, if you're doing integration tests then yea you need to use your db.

True. But integration testing needs to be done before the users hit the system.
Reply
#6

(08-14-2019, 01:45 AM)tgix Wrote: With CI4 I try to do the same, but find the following line from the $tests structure in Config\Database.php:67 a bit strange:
PHP Code:
'DBPrefix' => 'db_' // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE. 
 
I have already nuked my DB twice by setting this to blank so I'm really missing something here ;-)

Sorry - that note was for internal tests of the framework. If you're not running those, and just running the ones for your app, then change the prefix to whatever you need it to be.

( I should probably change that comment Smile )
Reply
#7

(08-15-2019, 11:34 AM)kilishan Wrote:
(08-14-2019, 01:45 AM)tgix Wrote: With CI4 I try to do the same, but find the following line from the $tests structure in Config\Database.php:67 a bit strange:
PHP Code:
'DBPrefix' => 'db_' // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE. 
 
I have already nuked my DB twice by setting this to blank so I'm really missing something here ;-)

Sorry - that note was for internal tests of the framework. If you're not running those, and just running the ones for your app, then change the prefix to whatever you need it to be.

( I should probably change that comment Smile  )

An extra note that the tests are deleting the tables with this prefix after completion maybe in place (in CIDatabaseTestCase::tearDown()).
If DBPrefix is blank this will delete all tables.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB