laravel 使用 Codeception 功能测试来 POST 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17344540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Codeception functional tests to POST data
提问by Chris Armitage
I want to create a test which will directly post data to a Laravel page to test it handling bad data.
我想创建一个测试,将数据直接发布到 Laravel 页面以测试它处理不良数据。
I can run an acceptance test starting at the page with the form on, call $I->click('Search');
and the page processes the data as expected.
我可以从带有表单的页面开始运行验收测试,调用$I->click('Search');
和页面按预期处理数据。
Reading the introduction to Functional Tests on the Codeception website, it states
阅读 Codeception 网站上的功能测试介绍,它指出
In simple terms we set $_REQUEST, $_GET and $_POST variables, then we execute your script inside a test, we receive output, and then we test it.
In simple terms we set $_REQUEST, $_GET and $_POST variables, then we execute your script inside a test, we receive output, and then we test it.
This sounds ideal, set an array of POST data and fire it directly at the processing page. But I can't find any documentation for it. I've played with sendAjaxPostRequest
but it's not passing anything to $_POST.
这听起来很理想,设置一组 POST 数据并直接在处理页面上触发它。但我找不到它的任何文档。我玩过,sendAjaxPostRequest
但它没有将任何东西传递给 $_POST。
Is there a way I can test the pages in isolation like this?
有没有办法像这样单独测试页面?
回答by Chris Armitage
Turns out the solution lies with the Codeception REST module.
原来解决方案在于Codeception REST 模块。
Adding this to the functional.suite.yml
file allows you to write:
将此添加到functional.suite.yml
文件中允许您编写:
$I = new TestGuy($scenario);
$I->wantTo('check a page is resistant to POST injection');
$I->sendPOST(
'search',
array(
'startDate' => '2013-07-03',
'endDate' => '2013-07-10',
'exec' => 'some dodgy commands',
));
$I->see('Search results');
$I->dontSee('Dodgy command executed');
A little clunky, but it allows testing of a single page.
有点笨重,但它允许测试单个页面。
回答by Rob Gordijn
From the manual @ http://codeception.com/docs/04-AcceptanceTests
来自手册@ http://codeception.com/docs/04-AcceptanceTests
$I->submitForm('#update_form', array('user' => array( 'name' => 'Miles', 'email' => 'Davis', 'gender' => 'm' )));