7

次のエラーが発生し続けます。

There was 1 error:

1) Caremonk\MainSiteBundle\Tests\Controller\SecurityControllerFunctionalTest::testIndex
InvalidArgumentException: The current node list is empty.

しかし、localhost/login に移動すると、フォームに正しいコンテンツが入力されます。と言うセリフ…

$form = $crawler->selectButton('login')->form();

エラーを引き起こしています。テストの何が問題になっていますか?

機能テスト:

<?php

namespace Caremonk\MainSiteBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SecurityControllerFunctionalTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/login');

        $form = $crawler->selectButton('login')->form();
        $form['username'] = 'testActive';
        $form['password'] = 'passwordActive';
    }
}

小枝ビュー:

{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
    <div>{{ error.message }}</div>
{% endif %}

<form action="{{ path('caremonk_mainsite_login_check') }}" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />

    <label for="password">Password:</label>
    <input type="password" id="password" name="_password" />

    {#
        If you want to control the URL the user is redirected to on success (more details below)
        <input type="hidden" name="_target_path" value="/account" />
    #}

    <button type="submit">login</button>
</form>
4

4 に答える 4

19

Vineetの答えは正しいですが...私の意見では、ボタンから形を取るのは良い考えではありません。試してみてください:

$crawler->filter('form[name=yourAwesomeFormName]')->form();

またはクラス、IDなどの名前を変更します。1つのビューで、同じ送信値を持つ複数のフォームを作成できます

于 2014-07-30T11:04:10.227 に答える
7

symfony2 で機能テストを開始し、あなたの質問が未回答であることがわかりました。

selectButton メソッドは、実際のボタン テキストを文字列引数として受け取ります。したがって、ボタンが「フォームを送信してください」の場合、それがテキストになります。

$form = $crawler->selectButton('submit my form please')->form();

Andrew Stackoverflowによる回答をご覧ください

于 2013-10-21T06:35:12.373 に答える
0

わかりました、これが私がこのエラーを起こした理由とそれをどのように解決したかです。

( Goutteを使用) クローラーは GET を使用してブラウザーのように動作します。

したがって、次の場合:

$client = new Client();
$crawler = $client->request('GET', '/login');

クローラーが到達しようとしていますhttp://localhost/login

だから、基本的に、私はそれを次のように変更しました:

$client = new Client();
$crawler = $client->request('GET', 'http://site.dev/app_dev.php/login');

site.devは私の symfony プロジェクトの仮想ホストです。

それが役に立てば幸い!

于 2014-05-12T12:16:28.647 に答える
0

selectButton()nameまたはalt画像の値でボタンを選択します。

于 2013-07-13T08:38:51.287 に答える