How can I test the content of my controller via a unit test?
The controller has an execute-function. If my content phtml file contains "Hello World" or a page title "My Title" is configured in the xml file, my understanding is that I could write a unit test which checks that by a simple string comparison:
public function execute()
{
return $resultPage = $this->resultPageFactory->create();
}
Now … I do not know how to figure out how to get the html content or title out of the $resultPage element, and I also do not know how I can write a test function for this. Any ideas?
Best Answer
I have evaluated an answer with my colleagues: Unit tests are not really appropriate for the controller execute() method. Unit tests would test a method without references to other methods. The execute() method needs the entire Magento framework to run, and therefore can only be tested with an integration test.
Let's say we have a simple test controller
src/app/code/Mycompany/Mymodule/Controller/Adminhtml/Demo/Index.php
:We can then create a very simple integration test in
src/app/code/Mycompany/Mymodule/Test/Integration/Controller/Adminhtml/Demo/ViewTest.php
:Update: I just received a better version of the actual test function. AssertInstanceOf is probably the better way to test it: