我正在使用Behat 3和Mink为Laravel 5应用程序进行第一次验收测试。
该应用程序在Homestead VM下运行。
测试很简单,位于features/example.feature
文件中。这是测试:
Feature: Sample
In order to learn Behat
As a programmer
I need a simple url testing
Scenario: Registration
Given I am not logged in
When I go to the registration form
Then I will be automatically logged in
FeatureContext.php
有这个类:
<?php
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}
/**
* @Given I am not logged in
*/
public function iAmNotLoggedIn()
{
Auth::guest();
}
/**
* @When I go to the registration form
*/
public function iGoToTheRegistrationForm()
{
$this->visit(url('my/url'));
}
/**
* @Then I will be automatically logged in
*/
public function iWillBeAutomaticallyLoggedIn()
{
Auth::check();
}
}
然后,当我从命令行运行behat
时,我希望测试失败,因为没有my/url
路由(routes.php
文件只有{{1}的路由}})。
然而,测试返回绿色,这就是我所看到的:
/
当然,我正在使用Feature: Sample
In order to learn Behat
As a programmer
I need a simple url testing
Scenario: Registration # features/example.feature:7
Given I am not logged in # FeatureContext::iAmNotLoggedIn()
When I go to the registration form # FeatureContext::iGoToTheRegistrationForm()
Then I will be automatically logged in # FeatureContext::iWillBeAutomaticallyLoggedIn()
1 scenario (1 passed)
3 steps (3 passed)
0m0.45s (22.82Mb)
包,这是laracasts/behat-laravel-extension
文件的内容:
beat.yml
非常感谢您的帮助!
答案 0 :(得分:1)
Behat非常简单。如果在执行时抛出异常,它会将步骤视为失败。它将步骤视为成功。
据我所知the docs,Auth::check()
如果用户未经过身份验证,则不会抛出异常。它只返回一个布尔值。
您的步骤应该更像以下所示:
/**
* @Then I will be automatically logged in
*/
public function iWillBeAutomaticallyLoggedIn()
{
if (!Auth::check()) {
throw new \LogicException('User was not logged in');
}
}
您的&#34;我转到注册表单&#34; 步骤成功,因为您没有真正验证您访问的网页是否是您希望加载的网页。同样,如果您访问的页面不正确,您应该抛出异常。