ユーザーを追加する機能をテストしたい。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);
}
}
私は私が正しい/品質な方法でそれを行う場合には、とても良いプログラマはその良いと思うだろうわかりません。 私が望む方法をテストしますか?これを端から端までテストしますか?コントローラーメソッドを呼び出して応答を確認しますか?コントローラーメソッドを呼び出すと私はもっと信じているでしょう。たとえば、返されたステータスコードやjson形式など、コモローラーで何かを破ることができるからです。
しかしbehatドキュメントで、私はテストだけで特定のクラスの例を見て - バスケットや棚:
http://docs.behat.org/en/v3.0/quick_intro_pt1.html
は、だから私は思った - 私はまた、特定のクラスをテストすることができます - リポジトリを。
そして私はまた、いくつかの偽のブラウザを使用する必要がありますコントローラメソッドを呼び出すために - http://mink.behat.org/en/latest
より多くの仕事かもしれません。