Behat pressButton不在laravel 5工作

时间:2015-01-24 06:09:02

标签: php testing behat 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');
}

当我点击网址时,填写表格并在浏览器中点击注册,我保存了一所新学校但是当我运行测试时,我似乎无法创建学校。 测试正在运行我可以在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)

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

1 个答案:

答案 0 :(得分:0)

您可以尝试替换:

$this->pressButton('register');

有类似的东西:

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

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

这也会更好; - )

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