Laravel unit testing assert functions always failing

793 views Asked by At

I have created a very basic unit testing class under Laravel root-->tests-->Unit-->SimpleTest.php

Inside the file i have import the controller class that i need to test. And test function is like this.

class SimpleTest extends TestCase
{
   public function testLoadUsers()
   {
     $controller_obj = new UsersController;
     $data_status = $controller_obj->load_users();
        if($data_status != null){
           $this->assertTrue(true);
         }
        else{
           $this->assertTrue(false);
         }
    }

  }

I executed this test case in Artisan console like this,

php vendor/phpunit/phpunit/phpunit tests/Unit/SimpleTest.php

Always this fails. My controller function returning data as well without an issue. I tested without defining any condition and just,

$this->assertTrue(true);

Then it works. So i assume there is no any issue with the phpunit test command as well.

1

There are 1 answers

5
tatsuya kanemoto On

I'm sorry, this is not a direct answer.
I think maybe you should check the value of $data_status by this.

class SimpleTest extends TestCase
{
   public function testLoadUsers()
   {
     $controller_obj = new UsersController;
     $data_status = $controller_obj->load_users();
     $this->expectOutputString('Array');
     print data_status;
   }
}