如何测试Laravel 5控制器方法 [英] How to test Laravel 5 controllers methods

查看:180
本文介绍了如何测试Laravel 5控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有Laravel 5控制器方法:

We have Laravel 5 controllers method:

public function getInput()
{
    $input = \Request::all();
    $links = $input['links'];
    $this->startLinks =  explode("\n", $links);
    return $this;
}

我们如何测试这种单一方法?如何将带有数据的POST请求传递给此方法?以及如何在我的测试方法中创建该控制器类的实例?

How can we test this single method? How to pass POST request with data to this method? And how to create instance of this controller class in my test method?

推荐答案

这看起来像是一种可能不应该属于控制器的方法.如果您认真对待测试,我强烈建议您阅读存储库模式.在进行测试时,将所有内容从控制器中提取出来将使您的工作变得更加轻松.=

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.=

尽管如此,这仍然是可以测试的.主要思想是弄清楚需要测试什么,并且仅对其进行测试.这意味着我们不在乎依赖项在做什么,只在乎它们在做什么并返回该方法其余部分将需要的东西.在这种情况下,它是Request门面.

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));
}

这篇关于如何测试Laravel 5控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆