繁体   English   中英

Behat pressButton在Laravel 5中不起作用

[英]Behat pressButton not working in laravel 5

https://laracasts.com/lessons/laravel-5-and-behat-driving-authentication

这是我正在测试的Jeoffery Way的教程

这是我要测试的方案

Scenario: Adding a new school
When I add a new school with "Narendra School" "This is a cool school"
Then I should see "Add School"
And a school is created

这是我的功能上下文文件,

/**
* @When I add a new school with :arg1 :arg2
*/
public function iAddANewSchoolWith($name , $description )
{
    $this->visit('school/create');
    $this->fillField( 'name', $name );
    $this->fillField( 'description', $description );
    $this->pressButton('register');
}

/**
* @Then a school is created
*/
public function aSchoolIsCreated()
{
   $school = School::all();
   PHPUnit::assertEquals($school->isEmpty(), false );
}

这是我的html代码

<form method="post">
<h2>Add School</h2>

    <input type="hidden" name="_token" value="{!! Session::getToken() !!}"/>

    <input type="text" name="name" placeholder="Name">
    <textarea name="description" placeholder="Description"></textarea>
    <button name="register">Register</button>
</form>

这是我用来创建对象的控制器方法

function create( ) {

   if ( \Request::isMethod( 'post' ) ) {

     $this->school->add( \Input::all() );// I am using a repository and dependency injection so this code is fine, it will add a school
   }

   return view('school::school.add');
}

当我点击URL,填写表格并在浏览器中点击注册时,我保存了新学校,但是当我运行测试时,我似乎无法创建学校。 测试正在运行,当我执行printLastResponse()时,我可以看到html。

Feature: Testing
    In order to teach Behat
    As a teacher
    I want to add a new school

  Scenario: Adding a new school                                            # features\example.feature:6
    When I add a new school with "Narendra School" "This is a cool school" # FeatureContext::iAddANewSchoolWith()
    Then I should see "Add School"                                         # FeatureContext::assertPageContainsText()
    And a school is created                                                # FeatureContext::aSchoolIsCreated()
      Failed asserting that false matches expected true.

--- Failed scenarios:

    features\example.feature:6

1 scenario (1 failed)
3 steps (2 passed, 1 failed)
0m3.10s (26.24Mb)

这是我的终端日志,我在做什么错?

您可以尝试替换:

$this->pressButton('register');

用类似的东西:

$page = $this->getSession()->getPage();
$buttonElement = $page->find('css',".//button[@name='register']");
$buttonElement->click();

使用Selenium驱动程序时,有时您可以在Selenium Server日志中找到线索,或者至少在出现问题时看到异常。

同样这会更好;-)

PHPUnit::assertNotEmpty($school->isEmpty());

暂无
暂无

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

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