如何在laravel测试用例中模拟xmlHttpRequests? [英] How to simulate xmlHttpRequests in a laravel testcase?

查看:253
本文介绍了如何在laravel测试用例中模拟xmlHttpRequests?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新见下文

我的控制器区分ajax和其他请求(使用Request::ajax()作为条件).效果很好,但我想知道是否有一种方法可以对处理请求的控制器进行单元测试.测试应该是什么样的? 大概是这样,但是不起作用...

My controllers distinguish between ajax and other requests (using Request::ajax() as a condition). That works quite fine but I wonder if there is a way of unit testing the controllers handling the the requests. How should a test look like? Something like this probably but it doesn't work ...

<?php

    class UsersControllerTest extends TestCase
        {


            public function testShowUser()
            {
                $userId = 1;
                $response = $this->call('GET', '/users/2/routes', array(), array(), array(
                    'HTTP_CUSTOM' => array(
                        'X-Requested-With' => 'XMLHttpRequest'
                    )
                ));


            }
        }

更新

我找到了解决方案.可能是.由于我对测试Request类的正常功能不感兴趣(很可能已经对Laravel,Symfony等提供的所有本机类进行了足够的单元测试),最好的方法可能是模拟其ajax方法.像这样:

I kind of found a solution. Maybe. Since I am not interested in testing the proper functionality of the Request class (very likely all native classes provided by Laravel, Symfony, etc. are enough unit tested already) best way might be to mock its ajax method. Like this:

        public function testShowUser()
        {

            $mocked = Request::shouldReceive('ajax')
                ->once()
                ->andReturn(true);
            $controller = new UsersCustomRoutesController;
            $controller->show(2,2);
        }

因为使用Testcase类的call方法时使用的是真正的Request类,而不使用其模拟替代物,所以我不得不实例化当手动输入指定路线时调用的方法.但是我认为这是可以的,因为我只想控制Request::ajax()条件内的表达式按此测试的预期运行.

Because the real Request class and not its mocked substitute is used when using the call method of the Testcase class I had to instantiate the method which is called when the specified route is entered by hand. But I think that is okay because I just want to control that the expressions inside the Request::ajax() condition work as expected with this test.

推荐答案

您需要使用HTTP_为实际标头添加前缀,而无需使用HTTP_CUSTOM:

You need to prefix the actual header with HTTP_, no need to use HTTP_CUSTOM:

$server = array('HTTP_X-Requested-With' => 'XMLHttpRequest');
$this->call('get', '/ajax-route', array(), array(), $server);

IMO看起来更好的替代语法:

Alternative syntax which looks a bit better IMO:

$this->client->setServerParameter('HTTP_X-Requested-With', 'XMLHttpRequest');
$this->call('get', '/ajax-route');

以下是JSON标头(Request::isJson()Request::wantsJson())的一些类似代码示例:

Here are some similar code examples for JSON headers (Request::isJson() and Request::wantsJson()):

$this->client->setServerParameter('HTTP_CONTENT_TYPE', 'application/json');
$this->call('get', '/is-json');

$this->client->setServerParameter('HTTP_ACCEPT', 'application/json');
$this->call('get', '/wants-json');

这是您可以放入TestCase的有用的辅助方法:

Here's a useful helper method you can put in your TestCase:

protected function prepareAjaxJsonRequest()
{
    $this->client->setServerParameter('HTTP_X-Requested-With', 'XMLHttpRequest');
    $this->client->setServerParameter('HTTP_CONTENT_TYPE', 'application/json');
    $this->client->setServerParameter('HTTP_ACCEPT', 'application/json');
}

这篇关于如何在laravel测试用例中模拟xmlHttpRequests?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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