How to test Laravel 5 controllers methods

后端 未结 2 2059
没有蜡笔的小新
没有蜡笔的小新 2021-02-14 07:52

We have Laravel 5 controllers method:

public function getInput()
{
    $input = \\Request::all();
    $links = $input[\'links\'];
    $this->startLinks =  exp         


        
2条回答
  •  情歌与酒
    2021-02-14 08:18

    This looks like a method which probably shouldn't belong in a controller. If you are serious about testing, I'd highly recommend reading up on the repository pattern. When testing, it will make your life a lot easier to have everything abstracted out of the controllers.=

    With that said though, this is still very testable. The main idea is to figure out what needs to be tested and ONLY test that. This means we don't care what the dependencies are doing, only that they are doing and returning something which the rest of the method will need. In this case, it's the Request facade.

    Then you want to make sure the variables are set appropriately and that the method returned an instance of that class. It actually ends up being pretty straight forward.

    Should look something like this...

    public function testGetInput()
    {
        $requestParams = [
            'links' => "somelink.com\nsomeotherlink.com\nandanotherlink.com\ndoesntmatter.com"
        ];
    
        // Here we are saying the \Request facade should expect the all method to be called and that all method should
        // return some pre-defined things which we will use in our asserts.
        \Request::shouldReceive('all')->once()->andReturn($requestParams);
    
        // Here we are just using Laravel's IoC container to instantiate your controller.  Change YourController to whatever
        // your controller is named
        $class = App::make('YourController');
    
        // Getting results of function so we can test that it has some properties which were supposed to have been set.
        $return = $class->getInput();
    
        // Again change this to the actual name of your controller.
        $this->assertInstanceOf('YourController', $return);
    
        // Now test all the things.
        $this->assertTrue(isset($return->startLinks));
        $this->assertTrue(is_array($return->startLinks));
        $this->assertTrue(in_array('somelink.com', $return->startLInks));
        $this->assertTrue(in_array('nsomeotherlink.com', $return->startLInks));
        $this->assertTrue(in_array('nandanotherlink.com', $return->startLInks));
        $this->assertTrue(in_array('ndoesntmatter.com', $return->startLInks));
    }
    

提交回复
热议问题