سؤال

In a simple and slightly modified functional test generated by the CRUD generator, a crawler test fails incorrectly. This is determined by examining the content in an external file created before the assertion. And by element inspection of the page. And by manual running of jquery code $("td:contains('Test')").length; So why does the test fail?

Test class:

class ApplianceControllerTest extends WebTestCase {

    private $client;

    public function __construct() {
        $this->client = static::createClient(array(), array(
         ...
        ));
        $this->client->followRedirects();
    }

    public function testCompleteScenario() {
        // Create a new entry in the database
        $crawler = $this->client->request('GET', '/appliance/');
        $crawler = $this->client->click($crawler->selectLink('Create a new entry')->link());

        // Fill in the form and submit it
        $form = $crawler->selectButton('Create')->form(array(
            'appliance[appliance]' => 'Test',
                // ... other fields to fill
        ));

        $this->client->submit($form);
//        $crawler = $this->client->followRedirect();
        $content = $this->client->getResponse()->getContent();
        file_put_contents('somefile', $content);

        // Check data in the show view
        $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")');
}

Output:

Missing element td:contains("Test") Failed asserting that 0 is greater than 0.

Excerpt of captured content:

<div class="title">Appliance</div>
<div class="width40">

    <table class="record_properties">
        <tbody>
            <tr>
                <th>Id</th>
                <td>14</td>
            </tr>
            <tr>
                <th>Appliance</th>
                <td>Test</td>
            </tr>
            <tr>
                <th>Enabled</th>
                <td>Yes</td>
            </tr>
        </tbody>
    </table>
هل كانت مفيدة؟

المحلول

Summarizing the comment thread:

You have commented out $crawler = $this->client->followRedirect(); due to the error "LogicException: The request was not redirected." This error indicates that the form submission is not successful.

As you discovered, removing the line $this->client->followRedirects(); from __construct() and reinstate the line $crawler = $this->client->followRedirect(); fixes your test.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top