简体   繁体   English

在PHPUnit和Laravel测试中使用Ajax Request

[英]Using Ajax Request with PHPUnit and Laravel testing

I am doing tests in Laravel with phpunit. 我正在使用phpunit在Laravel进行测试。 I run a test that inserts that into a textbox and press a button. 我运行一个测试,将其插入文本框并按下按钮。 Then, theres an Ajax Post that sends the info to the server and returns the response. 然后,这是一个Ajax Post,它将信息发送到服务器并返回响应。 If I return a string has the following code, in the test I should get that string correct? 如果我返回一个字符串有以下代码,在测试中我应该得到该字符串正确吗?

I have the following controller: 我有以下控制器:

class MyController extends Controller{
    public static function createNewComplaintStep_2(){
          return "error";
    }
}

The route: 路线:

Route::post('/createNewComplaint_Step2', 'MyController@createNewComplaintStep_2');

The view with form and ajax method: 使用form和ajax方法的视图:

<form id="formToSubmit" data-value="createNewComplaint_Step2" class="form-horizontal">
                {!! csrf_field() !!}
                <div class="form-group">
                    <label class="col-md-4 control-label">Customer:</label>
                    <div class="col-md-3">
                        <input class="form-control" name="customer_name" required>                            
                    </div>
                 </div>
         <button type="submit" class="btn btn-success" name="subBTN"\>
         </form>
(...)
<script>
$( '#formToSubmit' ).on( 'submit', function(e) {
    var href = $(this).data('value');   
    e.preventDefault();     
    $.ajax({
        type: "POST",
        url: href,
        data: $(this).serialize(),
        success: function( msg ) {
            $("#container-fluid").html(msg);
            $('.se-pre-con').hide();
        }
    });
});
</script>

The phpunit test: phpunit测试:

$this->visit('/createNew')
            ->type('NAME', 'customer_name')
            ->press('subBTN')
            ->see('error');

Is there something wrong? 有什么不对? Because if I run the code, I get error 因为如果我运行代码,我会收到错误

Failed asserting that the page contains the HTML [error]. 声明页面包含HTML [错误]的失败。 Please check the content above 请检查上面的内容

The content above is the view that is above . 上面的内容是上面的视图 It seems that the ajax request is not working with phpunit. 似乎ajax请求不能与phpunit一起使用。 Any suggestions?? 有什么建议么??

thanks in advance! 提前致谢!

You are working with jquery Ajax, which is a client side scripting language , basically executed in the browser at runtime it cannot be used by Laravel in tests. 您正在使用jquery Ajax,它是一种客户端 脚本 语言 ,基本上在运行时在浏览器中执行,Laravel在测试中无法使用它。

Here your Ajax is not even execute so how can you get the string that's why this error is coming. 在这里你的Ajax甚至都没有执行,所以如何获得字符串就是这个错误即将发生的原因。

But you can try some extensions/drivers for laravel that can enable this feature. 但您可以尝试使用laravel的某些扩展/驱动程序来启用此功能。
Just check out Selenium:- https://laracasts.com/discuss/channels/testing/has-anyone-tried-laravel-integrated-package-in-laravel-52 查看Selenium: - https://laracasts.com/discuss/channels/testing/has-anyone-tried-laravel-integrated-package-in-laravel-52

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM