2016-07-24 95 views
0

我想测试添加用户的功能。可以用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

这可能是更多的工作。

回答

0

是的,你可以测试你想要的东西,但是最好用脚本/流程定义一些功能。

如果你想要并且需要首先进行测试,那么做,否则测试你需要测试的东西。

与控制器方法调用相关,您应该做什么是合乎逻辑的,并为您的套件带来价值,您需要牢记的是处理异常并抛出对您有意义的适当异常。

尝试制定一个快速计划,您也可以与团队中的人员讨论如何为自动化方法添加一些有价值的信息,包括您需要涵盖的内容。

要记住的其他事项:看看水貂司机和页面对象。

相关问题