开发者

Need help on writing php testing files to test my PHP controller class

开发者 https://www.devze.com 2022-12-19 19:11 出处:网络
followed by my previous website development,we have built several controller classs corresponding to entities that will appear in our website development.and our website is very simple:just a shopping

followed by my previous website development,we have built several controller classs corresponding to entities that will appear in our website development.and our website is very simple:just a shopping website for customers to book bus for travel.

Now I am assigned a challenging task that need to be done,that is ,for a single controller class,I need to write a test case to test whether it is working properly.

For example,we have a controller class called "JobsController",like:

models = array( 'jobs' => new Jobs开发者_如何学Go() ); } function __destruct() { parent::__destruct(); } function addJob( $name , $desc ) { if( $name == '' ) { return false; }; $params = array( 'name' => $name , 'description' => $desc ); return $this->models['jobs']->add( $params ); } function modifyJob( $jid , $name , $desc ) { if( $jid == '' || $name == '' ) { return false; }; $params = array( 'name' => $name , 'description' => $desc ); return $this->models['jobs']->modify( $jid , $params ); } function removeJob( $jid ) { if( $jid == '' ) { return false; }; return $this->models['jobs']->remove( $jid ); } function getJob( $jid ) { return $this->models['jobs']->getInfo( $jid ); } function getAllJobs() { return $this->models['jobs']->getAll(); } } ?>

and all other Controller class are almost the same,just with name changes.

Now I really need you guys help me a bit how on to write a simple php file to test whether this Controller Class is working properly.After studying your code,I could get my hands on it to work with the rest controllers myself.

Many thank!


When writing test cases you are basically testing a before and after scenario. Before the test is run you want to initialize a clean environment (every time) to ensure you know what to expect. Next you'll run your methods and test what the environment is like afterward, confirming whether or not the action you took had the desired affect.

For example to test your modifyJob method you want to create an environment with a job, call the modifyJob method with test values, and then call getJob on the same job and ensure that the return value has values matching what you passed in to modifyJob:

function testModifyJob() {
  // create clean, known environment
  $controller = new JobsController();
  $job_id = $controller->addJob('name', 'description');

  // run the action
  $controller->modifyJob($job_id, 'new name', 'new description');

  //test the results
  $job = $controller->getJob($job_id);
  $result = $job['name'] == 'new name' && $job['description'] == 'new description';

  return $result;
}

For testing your controller you're going to want to write test cases like the one above for each of the methods the class provides to ensure that the object can be trusted within your system.

When writing your test cases you'll most likely have a need to create new methods on your controller just for your tests, go ahead and create these (if your assignment permits). It's very common for test cases to bring this up and usually helps round out the available functionality of your classes by bringing these nuances to light (one of the reasons test-driven development has such a strong following). An example of this would be creating a hasJob($job_id) method that would test if your jobs collection contains a specific job, allowing you to test functions such as deleteJob().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号