Phpunit decoupling test Hello everyone. I am currently studying phpunit testing.
This problem cannot be solved.
Currently, the development framework zend framework 1 is required to decouple the model layer when testing the controller layer.
For example:
public function phpunitAction(){ $enterpriseModel = new Model_Enterprise(); $sum = $enterpriseModel->getTotal(); print 'asd'; file_put_contents('xx.log',"suma is ".$sum[0]['total']."\r\n",FILE_APPEND); }
When I write a test case, I do not need to query data in the database during instantiation, but use the mock object of phpunit to imitate an object.
I read the document and wrote the test case myself, but it seems a problem.
Public function testPhpunitAction () {$ oClientMock = $ this-> getMock ('Model _ enter '); $ res = array (0 => array ('total' => 22); $ oClientMock-> expects ($ this-> once ()) // set the number of times-> method ('gettotal') // Set the method-> will ($ this-> returnValue ($ res )); // set the return value of the method $ enterpriseModel = new Model_Enterprise (); $ sum = $ enterpriseModel-> getTotal (); $ user_info ['User _ name'] = 'root '; $ user_info ['real _ name'] = 'root'; $ user_info ['id'] = 1; $ user_info ['group _ id'] = array (0 => 1); $ session = new Zend_Session_Namespace ('userinfo'); $ session-> user_info = $ user_info; $ params = array ('action' => 'phpunit ', 'controller' => 'index', 'module' => 'analyze '); $ url = $ this-> url ($ this-> urlizeOptions ($ params); $ this-> dispatch ($ url );}
But this does not work.
Beg for advice.
Reply to discussion (solution)
1. $ oClientMock a Model_Enterprise object
2. the second parameter of getMock needs to be passed into the method that needs to be copied. Otherwise, the original method will not be copied.
$ OClientMock = $ this-> getMock ('Model _ enter', array ('gettotal ')); $ res = array (0 => array ('total' => 22); $ oClientMock-> expects ($ this-> once ()) // set the number of times-> method ('gettotal') // Set the method-> will ($ this-> returnValue ($ res )); // Set the method return value $ sum = $ oClientMock-> getTotal ();
Hi, I'm the owner of http://bbs.csdn.net/topics/390923963and asked you a question about phpunit.
Reading your reply is very helpful to me.
I would like to ask how to perform a decoupling test on the controller layer for the MVC framework.
For example, I have a controller.
Class EnterPrise_AnnounceController extends ZendX_Controller_Action
{
Public function needchecknumAction (){
$ AnnounceModel = new Model_Announce ();
$ Info = $ announceModel-> getTotal ("status IN ('". Model_Announce: STATUS_PENDING ."')");
$ Info ['result'] = $ info ['total'];
Return Common_Protocols: generate_json_response ($ info );
}
}
How can I use MOCK in a test case to call my mock instead of calling the model layer in the framework in the controller?
Hi, I'm the owner of http://bbs.csdn.net/topics/390923963and asked you a question about phpunit.
Reading your reply is very helpful to me.
I would like to ask how to perform a decoupling test on the controller layer for the MVC framework.
For example, I have a controller.
Class EnterPrise_AnnounceController extends ZendX_Controller_Action
{
Public function needchecknumAction (){
$ AnnounceModel = new Model_Announce ();
$ Info = $ announceModel-> getTotal ("status IN ('". Model_Announce: STATUS_PENDING ."')");
$ Info ['result'] = $ info ['total'];
Return Common_Protocols: generate_json_response ($ info );
}
}
How can I use MOCK in a test case to call my mock instead of calling the model layer in the framework in the controller?
MOCK cannot simulate a class. it can only simulate objects. you need to replace the new database model in the controller with the set/get method into a mock.
Thank you. it is very helpful.