Behat 3 with Laravel 5: 验收测试通过但不应该
Behat 3 with Laravel 5: acceptance test passes but it should not
我正在使用 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
有这个 class:
<?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
文件只有/
).
的路线
但是,测试returns绿色,我看到的是这样的:
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
文件的内容:
default:
extensions:
Laracasts\Behat: ~
Behat\MinkExtension:
default_session: laravel
laravel: ~
非常感谢您的帮助!
行为很简单。如果在执行某个步骤时抛出异常,它将认为该步骤失败。否则将步骤视为成功。
据我从 the docs 得知,如果用户未通过身份验证,Auth::check()
不会抛出异常。它只是 returns 一个布尔值。
你的步骤应该更像下面这样实现:
/**
* @Then I will be automatically logged in
*/
public function iWillBeAutomaticallyLoggedIn()
{
if (!Auth::check()) {
throw new \LogicException('User was not logged in');
}
}
您的 "I go to the registration form" 步骤成功,因为您没有真正验证您访问的页面是否是您预期加载的页面。同样,如果您访问的页面不正确,您应该抛出异常。
我正在使用 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
有这个 class:
<?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
文件只有/
).
但是,测试returns绿色,我看到的是这样的:
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
文件的内容:
default:
extensions:
Laracasts\Behat: ~
Behat\MinkExtension:
default_session: laravel
laravel: ~
非常感谢您的帮助!
行为很简单。如果在执行某个步骤时抛出异常,它将认为该步骤失败。否则将步骤视为成功。
据我从 the docs 得知,如果用户未通过身份验证,Auth::check()
不会抛出异常。它只是 returns 一个布尔值。
你的步骤应该更像下面这样实现:
/**
* @Then I will be automatically logged in
*/
public function iWillBeAutomaticallyLoggedIn()
{
if (!Auth::check()) {
throw new \LogicException('User was not logged in');
}
}
您的 "I go to the registration form" 步骤成功,因为您没有真正验证您访问的页面是否是您预期加载的页面。同样,如果您访问的页面不正确,您应该抛出异常。