Test presenteru nacita spatny template

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
zeleny_raoul
Člen | 3
+
0
-

Ahoj,

Bojuju poprve s testy aplikace. Snazim se otestovat metodu renderHistory() v presenteru, ale tester mi v jednom kuse nacita spatny template.

Toto je test:

<?php
$container = require __DIR__ . '/../bootstrap.php';

class HomepagePresenterTest extends Tester\TestCase {

    private $presenter;
    private $signPresenter;
    public $database;
    public $userModel;
    public $requestModel;
    private $container;

    function __construct(Nette\DI\Container $container) {
        $this->container = $container;
        $presenterFactory = $container->getByType('Nette\Application\IPresenterFactory');
        $this->presenter = $presenterFactory->createPresenter('Front:Homepage');
        $this->presenter->autoCanonicalize = FALSE; // not redirect to canonical URLs
        $this->database = $container->getByType('Nette\Database\Connection');
        $this->userModel = $container->getByType('App\Repository\User');
        $this->requestModel = $container->getByType('App\Repository\Request');
    }

    function testRenderHistory() {
        $this->presenter->getUser()->login('e1', '123456');
        $request = new Nette\Application\Request('Homepage', 'GET', array('action' => 'history'));
        $response = $this->presenter->run($request);
        // verify that the response is a template
        Assert::true($response instanceof \Nette\Application\Responses\TextResponse);
        Assert::true($response->getSource() instanceof \Nette\Application\UI\ITemplate);

        // generate souce code
        $html = (string) $response->getSource();
        $dom = Tester\DomQuery::fromHtml($html);

        // always fails -- Assert::true($dom->has('span[id="desired-id"]'));
        Assert::same(FALSE, $html);
    }
?>

Ten Assert::same(FALSE, $html) jsem vlozil, aby tester udelal dump sablony. Bohuzel to neni sablona history.latte, ale default.latte.

Mel jsem pocit, ze musi zlobit routovani, takze jsem do presenteru natvrdo narval vyjimku, ktera mi vypise zdrojak sablony.

<?php
class HomepagePresenter extends BasePresenter {

   public function renderHistory() {
        $dealerId = ($this->user->getIdentity()->data['DealerID']);
        $history = $this->model->getLockedRequests($dealerId);
        $this->template->history = $history;
        throw new \Exception($this->template);
    }
?>

Vyjimku to vyhodi, takze tester opravdu vola renderHistory(), ale text vyjimky je opravdu zdrojak default.latte misto history.latte, coz absoulutne nechapu. Pri rucnim testovani v prohlizeci vsechno slape, jak ma. Co delam spatne?

zeleny_raoul
Člen | 3
+
0
-

Kdyz natvrdo nastavim template tako, vsechno najednou projde v poradku a vyjimka vyhazuje spravny text sablony:

<?php
    public function renderHistory() {
        $this->template->setFile(__DIR__ . "/../templates/Homepage/history.latte");
        $dealerId = ($this->user->getIdentity()->data['DealerID']);
        $history = $this->model->getLockedRequests($dealerId);
        $this->template->history = $history;
        throw new \Exception($this->template);
    }
?>