我想测试添加用户的功能。可以用behat测试版本库吗?
我写了下面的情况:
Feature: Add users
In order to have users
As an admin
I need to be able to add users to database
Rules:
- User has a name
Scenario: Adding user with name 'Jonas'
Given There is no user 'Jonas'
When I add the user with name 'Jonas'
Then I should have user 'Jonas' in a system
而且下面的测试:
<?php
use AppBundle\Repository\UserRepository;
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 AppBundle\Entity\User;
use Symfony\Component\Config\Definition\Exception\Exception;
/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context, SnippetAcceptingContext
{
private $userRepository;
/**
* 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(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @Given There is no user :arg1
*/
public function thereIsNoUser($arg1)
{
$user = $this->userRepository->findOneBy(['name' => $arg1]);
if ($user) {
$this->userRepository->delete($user);
}
}
/**
* @When I add the user with name :arg1
*/
public function iAddTheUserWithName($arg1)
{
$user = new User($arg1);
$this->userRepository->add($user);
}
/**
* @Then I should have user :arg1 in a system
*/
public function iShouldHaveUserInASystem($arg1)
{
$user = $this->userRepository->findOneBy(['name' => $arg1]);
if (!$user) {
throw new Exception('User was not added');
}
$this->userRepository->delete($user);
}
}
我不知道如果我这样做是正确/品质的生活方式,那么好的程序员会认为它好。 我测试我想要的方式吗?或者我应该从头到尾对此进行测试 - 调用控制器方法并检查响应?调用控制器方法会测试更多我相信,因为我们也可以在cotroller中打破某些东西,例如返回的状态码或json格式。
但贝哈特文档中我看到了测试的只是特定的类的实例 - 篮和货架:
http://docs.behat.org/en/v3.0/quick_intro_pt1.html
所以我想 - 我还可以测试特定的类 - 库。
,并呼吁控制器方法我还需要使用一些假的浏览器 - http://mink.behat.org/en/latest
这可能是更多的工作。