Skip to content
This repository has been archived by the owner on May 27, 2023. It is now read-only.

controller tests do not load custom layout template #60

Closed
thomasvs opened this issue Dec 14, 2012 · 3 comments
Closed

controller tests do not load custom layout template #60

thomasvs opened this issue Dec 14, 2012 · 3 comments

Comments

@thomasvs
Copy link

I've written a controller test and am asserting that what's in my .phtml comes out in the test.

When I go to the page in the browser, the .phtml content is there.

When I assert for it in the test case, it's not.

The controller action is simply load/render.

The phtml is simply a php file printing one line.

The test is:

public function testDispatch()
{
    $this->dispatch('credex/standard/redirect');

    $this->assertRequestRoute('credex/standard/redirect');

    $this->assertLayoutBlockCreated('left');
    $this->assertLayoutBlockCreated('right');
    $this->assertLayoutBlockRendered('content');
    $this->assertLayoutBlockTypeOf('left', 'core/text_list');
    $this->assertLayoutBlockNotTypeOf('left', 'core/links');

    // FIXME: this doesn't work, so for now I work around it
    //$this->assertResponseBodyContains('Magento');
    $this->_assertResponseBodyContains('Magento');
    // FIXME: in a test, my template does not get invoked
    $this->_assertResponseBodyContains('Welcome to your custom module');

    return $this;
}

(I use a custom assertResponseBodyContains because I found the standard one did not work - are there any unit tests out there in a git repo that use it that I can check ?)

public function _assertResponseBodyContains($string)
{
$body = $this->getResponse()->getOutputBody();
$constraint = $this->stringContains($string);
$this->assertThat($body, $constraint);
}

Are there any known and working tests of controllers out there ? Preferably one with a custom layout template ?

@IvanChepurnyi
Copy link
Member

I tried to reproduce your bug but with no luck:
Here is my test case agains default home page:

    public function testDispatch()
    {

         $this->dispatch('');
         $this->assertRequestRoute('cms/index/index');

         $this->assertLayoutBlockCreated('left');
         $this->assertLayoutBlockCreated('right');
         $this->assertLayoutBlockRendered('content');
         $this->assertLayoutBlockTypeOf('left', 'core/text_list');
         $this->assertLayoutBlockNotTypeOf('left', 'core/links');

         $this->assertResponseBodyContains('Magento');
         $this->assertResponseBodyContains('Home Page');
         $this->assertResponseBodyContains('Compare Products');
         $this->assertResponseBodyNotContains('Non existing text');

         // This will fail!
         # $this->assertResponseBodyContains('Non existing text');
    }

However I have some thoughts about your problem in general, it might be possible that you haven't specified your design/theme configuration for test case, since tests are executed agains default database, which design settings are:

  • default/default.

So don't forget to specify your design in fixture, it is very easy:

config:
   default/design/package/name: your_package
   default/design/theme/default: your_theme

@thomasvs
Copy link
Author

thomasvs commented Jan 5, 2013

I'm not sure I follow 100%.

Why is it necessary to specify any fixtures at all for a test like
this ? Wouldn't it make more sense if it worked out of the box ?

If I don't have any custom design or theme, what are good values to use
for your_package and your_theme that will work out of the box with 1.7 ?

On Fri, 2013-01-04 at 11:18 -0800, Ivan Chepurnyi wrote:

I tried to reproduce your bug but with no luck:
Here is my test case agains default home page:

public function testDispatch()
{

     $this->dispatch('');
     $this->assertRequestRoute('cms/index/index');

     $this->assertLayoutBlockCreated('left');
     $this->assertLayoutBlockCreated('right');
     $this->assertLayoutBlockRendered('content');
     $this->assertLayoutBlockTypeOf('left', 'core/text_list');
     $this->assertLayoutBlockNotTypeOf('left', 'core/links');

     $this->assertResponseBodyContains('Magento');
     $this->assertResponseBodyContains('Home Page');
     $this->assertResponseBodyContains('Compare Products');
     $this->assertResponseBodyNotContains('Non existing text');

     // This will fail!
     # $this->assertResponseBodyContains('Non existing text');
}

However I have some thoughts about your problem in general, it might
be possible that you haven't specified your design/theme configuration
for test case, since tests are executed agains default database, which
design settings are:

  * default/default.

So don't forget to specify your design in fixture, it is very easy:

config:
default/design/package/name: your_package
default/design/theme/default: your_theme

Reply to this email directly or view it on GitHub.

The coming out tells the story.

savon - Saving your work to svn
https://apestaart.org/thomas/trac/

@IvanChepurnyi
Copy link
Member

You mentioned in the title of your ticket "load custom layout template", that supposed to mean that you have a custom layout file in custom theme that is somehow cannot not load template on particular page. That's why I suggested you to change current design package theme via fixture. It is mostly because test database is not a copy of your live db instance it is clean install though db upgrade scripts. If you haven't specified your theme in XML file or db upgrade, then it uses default Magento theme and your test case will not work!

Test framework is not affecting in any way the layout XML building and template file inclusion procedure and if you checked the following test case I provided to you, you'll see that assertResponseBodyContains() method is working without any problems.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants