我正在使用Behat 5和Mink对Laravel 3应用程序进行首次验收测试。
应用程序在 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
文件只有/
路由)。
但是,测试返回绿色,这就是我看到的:
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: ~
非常感谢您的任何帮助!
Behat非常简单。如果在执行步骤时引发异常,它将步骤视为失败。否则,它将步骤视为成功。
据我从文档中可以看出,如果用户未经过身份验证,Auth::check()
不会引发异常。它只是返回一个布尔值。
您的步骤应该更像下面这样实现:
/**
* @Then I will be automatically logged in
*/
public function iWillBeAutomaticallyLoggedIn()
{
if (!Auth::check()) {
throw new 'LogicException('User was not logged in');
}
}
您的"我转到注册表"步骤成功,因为您没有真正验证您访问的页面是否是您希望加载的页面。同样,如果您访问的页面不正确,您应该抛出异常。