如何使用Behat测试Laravel 5中的注册表?如何写一个功能?

时间:2016-06-19 21:17:43

标签: php unit-testing laravel bdd behat

我必须使用Behat 测试我的Larravel 应用程序, 我写了满足以下内容的Laravel应用程序:

  

用户在他/她的浏览器中打开Web应用程序

     

用户输入他/她的姓名并通过电子邮件发送到注册表单

     

用户点击'注册'表格下面的按钮

     

应用程序在数据库中注册用户(名称/电子邮件)

     

应用程序将用户重定向到包含“感谢”的页面   你“消息。***

我必须写 Behat功能,但我不知道从哪里开始?我读过关于 BDD 但仍然感到困惑。

对于非UTF-8等基本内容,也应该覆盖单元测试 输入,空表单和非字符串输入。

这样的事情?

    Feature: Registation form
        In order to test register on this site
        As a visitor
        I need to fill in the necessary info
    Scenario: 
        Given I am on the homepage
        When I fill in "username" with "Chrismo" 
        And I fill in "email" with "chris@chrismo.com"
        Then I should see "THank you"

2 个答案:

答案 0 :(得分:0)

您需要在features文件夹中写下所有功能文件。

例如,如果要为functional测试编写功能,可以创建名为welcome.feature的文件并将其放在features/functional文件夹中。

对于单元测试,Behat本身没有断言。所以,您可能想要使用PHPUnit的断言。由于PHPUnit捆绑了新的Laravel应用程序,它已经可用,并且所有人都需要做的是访问断言是导入Behat的FeatureContext类中的类,如下所示:

use PHPUnit_Framework_Assert as PHPUnit;

然后,您将可以访问断言并编写测试。

编辑:以下是您可以遵循的详细教程:https://blog.vinelab.net/building-reliable-apis-using-bdd-behant-laravel-5-949156a37e66#.yv89tcqkw

答案 1 :(得分:0)

我已经阅读了有关测试级别的内容:验收,功能,集成和单元。 我不确定前两者之间有什么区别。告诉我如果我错了,但在上面的例子中

接受程度将创建如下功能:

Feature: Registation form
    In order to test register on this site
    As a visitor
    I need to fill in the necessary info
Scenario: 
    Given I am on the homepage
    When I fill in "name" with "Chrismo" 
    And I fill in "email" with "chris@chrismo.com"
    And I press "Register"
    Then I should see "THank you".

关于功能性,必须做什么?也许"不知何故" check是保存在users表(数据库)中的用户吗?