1

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.

CodeCanyon
  • 929
  • 2
  • 11
  • 36

1 Answers1

0

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;
   }
}
tatsuya kanemoto
  • 1,848
  • 11
  • 22
  • I have tried this way as well. With print_r() and var_dump() both functions. Basically i'm getting the error on this line- $data_status = $controller_obj->load_users(); So code exits before going to print_r() function. – CodeCanyon Feb 20 '20 at 03:36
  • So the problem is not the test code. `$controller_obj->load_users();` is not working correctly in your testing environmnet. Please check [this](https://laravel.com/docs/5.8/testing#environment). – tatsuya kanemoto Feb 20 '20 at 03:52
  • I tried this as well. But still the issue persists. – CodeCanyon Feb 20 '20 at 06:18
  • What does `UsersController` depend on? If it needs a database connection, you must setup for testing environment. I don't know your laravel environment, so I can't help you farther. Please consider showing me your (minimal) environment, if you could. – tatsuya kanemoto Feb 20 '20 at 07:40
  • Ok, for the time being i don't need UserController. Assume just access through route such as $this->call('route_path');Anyway i have created env test file and defined database connection as well. Now i'm getting a 404 page. In commandline. – CodeCanyon Feb 21 '20 at 00:56